udp.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605
  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. * The User Datagram Protocol (UDP).
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  11. * Alan Cox, <alan@lxorguk.ukuu.org.uk>
  12. * Hirokazu Takahashi, <taka@valinux.co.jp>
  13. *
  14. * Fixes:
  15. * Alan Cox : verify_area() calls
  16. * Alan Cox : stopped close while in use off icmp
  17. * messages. Not a fix but a botch that
  18. * for udp at least is 'valid'.
  19. * Alan Cox : Fixed icmp handling properly
  20. * Alan Cox : Correct error for oversized datagrams
  21. * Alan Cox : Tidied select() semantics.
  22. * Alan Cox : udp_err() fixed properly, also now
  23. * select and read wake correctly on errors
  24. * Alan Cox : udp_send verify_area moved to avoid mem leak
  25. * Alan Cox : UDP can count its memory
  26. * Alan Cox : send to an unknown connection causes
  27. * an ECONNREFUSED off the icmp, but
  28. * does NOT close.
  29. * Alan Cox : Switched to new sk_buff handlers. No more backlog!
  30. * Alan Cox : Using generic datagram code. Even smaller and the PEEK
  31. * bug no longer crashes it.
  32. * Fred Van Kempen : Net2e support for sk->broadcast.
  33. * Alan Cox : Uses skb_free_datagram
  34. * Alan Cox : Added get/set sockopt support.
  35. * Alan Cox : Broadcasting without option set returns EACCES.
  36. * Alan Cox : No wakeup calls. Instead we now use the callbacks.
  37. * Alan Cox : Use ip_tos and ip_ttl
  38. * Alan Cox : SNMP Mibs
  39. * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
  40. * Matt Dillon : UDP length checks.
  41. * Alan Cox : Smarter af_inet used properly.
  42. * Alan Cox : Use new kernel side addressing.
  43. * Alan Cox : Incorrect return on truncated datagram receive.
  44. * Arnt Gulbrandsen : New udp_send and stuff
  45. * Alan Cox : Cache last socket
  46. * Alan Cox : Route cache
  47. * Jon Peatfield : Minor efficiency fix to sendto().
  48. * Mike Shaver : RFC1122 checks.
  49. * Alan Cox : Nonblocking error fix.
  50. * Willy Konynenberg : Transparent proxying support.
  51. * Mike McLagan : Routing by source
  52. * David S. Miller : New socket lookup architecture.
  53. * Last socket cache retained as it
  54. * does have a high hit rate.
  55. * Olaf Kirch : Don't linearise iovec on sendmsg.
  56. * Andi Kleen : Some cleanups, cache destination entry
  57. * for connect.
  58. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  59. * Melvin Smith : Check msg_name not msg_namelen in sendto(),
  60. * return ENOTCONN for unconnected sockets (POSIX)
  61. * Janos Farkas : don't deliver multi/broadcasts to a different
  62. * bound-to-device socket
  63. * Hirokazu Takahashi : HW checksumming for outgoing UDP
  64. * datagrams.
  65. * Hirokazu Takahashi : sendfile() on UDP works now.
  66. * Arnaldo C. Melo : convert /proc/net/udp to seq_file
  67. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  68. * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
  69. * a single port at the same time.
  70. * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
  71. * James Chapman : Add L2TP encapsulation type.
  72. *
  73. *
  74. * This program is free software; you can redistribute it and/or
  75. * modify it under the terms of the GNU General Public License
  76. * as published by the Free Software Foundation; either version
  77. * 2 of the License, or (at your option) any later version.
  78. */
  79. #define pr_fmt(fmt) "UDP: " fmt
  80. #include <asm/uaccess.h>
  81. #include <asm/ioctls.h>
  82. #include <linux/bootmem.h>
  83. #include <linux/highmem.h>
  84. #include <linux/swap.h>
  85. #include <linux/types.h>
  86. #include <linux/fcntl.h>
  87. #include <linux/module.h>
  88. #include <linux/socket.h>
  89. #include <linux/sockios.h>
  90. #include <linux/igmp.h>
  91. #include <linux/inetdevice.h>
  92. #include <linux/in.h>
  93. #include <linux/errno.h>
  94. #include <linux/timer.h>
  95. #include <linux/mm.h>
  96. #include <linux/inet.h>
  97. #include <linux/netdevice.h>
  98. #include <linux/slab.h>
  99. #include <net/tcp_states.h>
  100. #include <linux/skbuff.h>
  101. #include <linux/proc_fs.h>
  102. #include <linux/seq_file.h>
  103. #include <net/net_namespace.h>
  104. #include <net/icmp.h>
  105. #include <net/inet_hashtables.h>
  106. #include <net/route.h>
  107. #include <net/checksum.h>
  108. #include <net/xfrm.h>
  109. #include <trace/events/udp.h>
  110. #include <linux/static_key.h>
  111. #include <trace/events/skb.h>
  112. #include <net/busy_poll.h>
  113. #include "udp_impl.h"
  114. #include <net/sock_reuseport.h>
  115. #include <net/addrconf.h>
  116. struct udp_table udp_table __read_mostly;
  117. EXPORT_SYMBOL(udp_table);
  118. long sysctl_udp_mem[3] __read_mostly;
  119. EXPORT_SYMBOL(sysctl_udp_mem);
  120. int sysctl_udp_rmem_min __read_mostly;
  121. EXPORT_SYMBOL(sysctl_udp_rmem_min);
  122. int sysctl_udp_wmem_min __read_mostly;
  123. EXPORT_SYMBOL(sysctl_udp_wmem_min);
  124. atomic_long_t udp_memory_allocated;
  125. EXPORT_SYMBOL(udp_memory_allocated);
  126. #define MAX_UDP_PORTS 65536
  127. #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
  128. static int udp_lib_lport_inuse(struct net *net, __u16 num,
  129. const struct udp_hslot *hslot,
  130. unsigned long *bitmap,
  131. struct sock *sk,
  132. int (*saddr_comp)(const struct sock *sk1,
  133. const struct sock *sk2,
  134. bool match_wildcard),
  135. unsigned int log)
  136. {
  137. struct sock *sk2;
  138. kuid_t uid = sock_i_uid(sk);
  139. sk_for_each(sk2, &hslot->head) {
  140. if (net_eq(sock_net(sk2), net) &&
  141. sk2 != sk &&
  142. (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
  143. (!sk2->sk_reuse || !sk->sk_reuse) &&
  144. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
  145. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  146. (!sk2->sk_reuseport || !sk->sk_reuseport ||
  147. rcu_access_pointer(sk->sk_reuseport_cb) ||
  148. !uid_eq(uid, sock_i_uid(sk2))) &&
  149. saddr_comp(sk, sk2, true)) {
  150. if (!bitmap)
  151. return 1;
  152. __set_bit(udp_sk(sk2)->udp_port_hash >> log, bitmap);
  153. }
  154. }
  155. return 0;
  156. }
  157. /*
  158. * Note: we still hold spinlock of primary hash chain, so no other writer
  159. * can insert/delete a socket with local_port == num
  160. */
  161. static int udp_lib_lport_inuse2(struct net *net, __u16 num,
  162. struct udp_hslot *hslot2,
  163. struct sock *sk,
  164. int (*saddr_comp)(const struct sock *sk1,
  165. const struct sock *sk2,
  166. bool match_wildcard))
  167. {
  168. struct sock *sk2;
  169. kuid_t uid = sock_i_uid(sk);
  170. int res = 0;
  171. spin_lock(&hslot2->lock);
  172. udp_portaddr_for_each_entry(sk2, &hslot2->head) {
  173. if (net_eq(sock_net(sk2), net) &&
  174. sk2 != sk &&
  175. (udp_sk(sk2)->udp_port_hash == num) &&
  176. (!sk2->sk_reuse || !sk->sk_reuse) &&
  177. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
  178. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  179. (!sk2->sk_reuseport || !sk->sk_reuseport ||
  180. rcu_access_pointer(sk->sk_reuseport_cb) ||
  181. !uid_eq(uid, sock_i_uid(sk2))) &&
  182. saddr_comp(sk, sk2, true)) {
  183. res = 1;
  184. break;
  185. }
  186. }
  187. spin_unlock(&hslot2->lock);
  188. return res;
  189. }
  190. static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot,
  191. int (*saddr_same)(const struct sock *sk1,
  192. const struct sock *sk2,
  193. bool match_wildcard))
  194. {
  195. struct net *net = sock_net(sk);
  196. kuid_t uid = sock_i_uid(sk);
  197. struct sock *sk2;
  198. sk_for_each(sk2, &hslot->head) {
  199. if (net_eq(sock_net(sk2), net) &&
  200. sk2 != sk &&
  201. sk2->sk_family == sk->sk_family &&
  202. ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
  203. (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) &&
  204. (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  205. sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
  206. (*saddr_same)(sk, sk2, false)) {
  207. return reuseport_add_sock(sk, sk2);
  208. }
  209. }
  210. /* Initial allocation may have already happened via setsockopt */
  211. if (!rcu_access_pointer(sk->sk_reuseport_cb))
  212. return reuseport_alloc(sk);
  213. return 0;
  214. }
  215. /**
  216. * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
  217. *
  218. * @sk: socket struct in question
  219. * @snum: port number to look up
  220. * @saddr_comp: AF-dependent comparison of bound local IP addresses
  221. * @hash2_nulladdr: AF-dependent hash value in secondary hash chains,
  222. * with NULL address
  223. */
  224. int udp_lib_get_port(struct sock *sk, unsigned short snum,
  225. int (*saddr_comp)(const struct sock *sk1,
  226. const struct sock *sk2,
  227. bool match_wildcard),
  228. unsigned int hash2_nulladdr)
  229. {
  230. struct udp_hslot *hslot, *hslot2;
  231. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  232. int error = 1;
  233. struct net *net = sock_net(sk);
  234. if (!snum) {
  235. int low, high, remaining;
  236. unsigned int rand;
  237. unsigned short first, last;
  238. DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
  239. inet_get_local_port_range(net, &low, &high);
  240. remaining = (high - low) + 1;
  241. rand = prandom_u32();
  242. first = reciprocal_scale(rand, remaining) + low;
  243. /*
  244. * force rand to be an odd multiple of UDP_HTABLE_SIZE
  245. */
  246. rand = (rand | 1) * (udptable->mask + 1);
  247. last = first + udptable->mask + 1;
  248. do {
  249. hslot = udp_hashslot(udptable, net, first);
  250. bitmap_zero(bitmap, PORTS_PER_CHAIN);
  251. spin_lock_bh(&hslot->lock);
  252. udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
  253. saddr_comp, udptable->log);
  254. snum = first;
  255. /*
  256. * Iterate on all possible values of snum for this hash.
  257. * Using steps of an odd multiple of UDP_HTABLE_SIZE
  258. * give us randomization and full range coverage.
  259. */
  260. do {
  261. if (low <= snum && snum <= high &&
  262. !test_bit(snum >> udptable->log, bitmap) &&
  263. !inet_is_local_reserved_port(net, snum))
  264. goto found;
  265. snum += rand;
  266. } while (snum != first);
  267. spin_unlock_bh(&hslot->lock);
  268. } while (++first != last);
  269. goto fail;
  270. } else {
  271. hslot = udp_hashslot(udptable, net, snum);
  272. spin_lock_bh(&hslot->lock);
  273. if (hslot->count > 10) {
  274. int exist;
  275. unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
  276. slot2 &= udptable->mask;
  277. hash2_nulladdr &= udptable->mask;
  278. hslot2 = udp_hashslot2(udptable, slot2);
  279. if (hslot->count < hslot2->count)
  280. goto scan_primary_hash;
  281. exist = udp_lib_lport_inuse2(net, snum, hslot2,
  282. sk, saddr_comp);
  283. if (!exist && (hash2_nulladdr != slot2)) {
  284. hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
  285. exist = udp_lib_lport_inuse2(net, snum, hslot2,
  286. sk, saddr_comp);
  287. }
  288. if (exist)
  289. goto fail_unlock;
  290. else
  291. goto found;
  292. }
  293. scan_primary_hash:
  294. if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
  295. saddr_comp, 0))
  296. goto fail_unlock;
  297. }
  298. found:
  299. inet_sk(sk)->inet_num = snum;
  300. udp_sk(sk)->udp_port_hash = snum;
  301. udp_sk(sk)->udp_portaddr_hash ^= snum;
  302. if (sk_unhashed(sk)) {
  303. if (sk->sk_reuseport &&
  304. udp_reuseport_add_sock(sk, hslot, saddr_comp)) {
  305. inet_sk(sk)->inet_num = 0;
  306. udp_sk(sk)->udp_port_hash = 0;
  307. udp_sk(sk)->udp_portaddr_hash ^= snum;
  308. goto fail_unlock;
  309. }
  310. sk_add_node_rcu(sk, &hslot->head);
  311. hslot->count++;
  312. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  313. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  314. spin_lock(&hslot2->lock);
  315. if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
  316. sk->sk_family == AF_INET6)
  317. hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
  318. &hslot2->head);
  319. else
  320. hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
  321. &hslot2->head);
  322. hslot2->count++;
  323. spin_unlock(&hslot2->lock);
  324. }
  325. sock_set_flag(sk, SOCK_RCU_FREE);
  326. error = 0;
  327. fail_unlock:
  328. spin_unlock_bh(&hslot->lock);
  329. fail:
  330. return error;
  331. }
  332. EXPORT_SYMBOL(udp_lib_get_port);
  333. /* match_wildcard == true: 0.0.0.0 equals to any IPv4 addresses
  334. * match_wildcard == false: addresses must be exactly the same, i.e.
  335. * 0.0.0.0 only equals to 0.0.0.0
  336. */
  337. int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2,
  338. bool match_wildcard)
  339. {
  340. struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
  341. if (!ipv6_only_sock(sk2)) {
  342. if (inet1->inet_rcv_saddr == inet2->inet_rcv_saddr)
  343. return 1;
  344. if (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr)
  345. return match_wildcard;
  346. }
  347. return 0;
  348. }
  349. static u32 udp4_portaddr_hash(const struct net *net, __be32 saddr,
  350. unsigned int port)
  351. {
  352. return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
  353. }
  354. int udp_v4_get_port(struct sock *sk, unsigned short snum)
  355. {
  356. unsigned int hash2_nulladdr =
  357. udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
  358. unsigned int hash2_partial =
  359. udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
  360. /* precompute partial secondary hash */
  361. udp_sk(sk)->udp_portaddr_hash = hash2_partial;
  362. return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr);
  363. }
  364. static int compute_score(struct sock *sk, struct net *net,
  365. __be32 saddr, __be16 sport,
  366. __be32 daddr, unsigned short hnum, int dif)
  367. {
  368. int score;
  369. struct inet_sock *inet;
  370. if (!net_eq(sock_net(sk), net) ||
  371. udp_sk(sk)->udp_port_hash != hnum ||
  372. ipv6_only_sock(sk))
  373. return -1;
  374. score = (sk->sk_family == PF_INET) ? 2 : 1;
  375. inet = inet_sk(sk);
  376. if (inet->inet_rcv_saddr) {
  377. if (inet->inet_rcv_saddr != daddr)
  378. return -1;
  379. score += 4;
  380. }
  381. if (inet->inet_daddr) {
  382. if (inet->inet_daddr != saddr)
  383. return -1;
  384. score += 4;
  385. }
  386. if (inet->inet_dport) {
  387. if (inet->inet_dport != sport)
  388. return -1;
  389. score += 4;
  390. }
  391. if (sk->sk_bound_dev_if) {
  392. if (sk->sk_bound_dev_if != dif)
  393. return -1;
  394. score += 4;
  395. }
  396. if (sk->sk_incoming_cpu == raw_smp_processor_id())
  397. score++;
  398. return score;
  399. }
  400. static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
  401. const __u16 lport, const __be32 faddr,
  402. const __be16 fport)
  403. {
  404. static u32 udp_ehash_secret __read_mostly;
  405. net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret));
  406. return __inet_ehashfn(laddr, lport, faddr, fport,
  407. udp_ehash_secret + net_hash_mix(net));
  408. }
  409. /* called with rcu_read_lock() */
  410. static struct sock *udp4_lib_lookup2(struct net *net,
  411. __be32 saddr, __be16 sport,
  412. __be32 daddr, unsigned int hnum, int dif,
  413. struct udp_hslot *hslot2,
  414. struct sk_buff *skb)
  415. {
  416. struct sock *sk, *result;
  417. int score, badness, matches = 0, reuseport = 0;
  418. u32 hash = 0;
  419. result = NULL;
  420. badness = 0;
  421. udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
  422. score = compute_score(sk, net, saddr, sport,
  423. daddr, hnum, dif);
  424. if (score > badness) {
  425. reuseport = sk->sk_reuseport;
  426. if (reuseport) {
  427. hash = udp_ehashfn(net, daddr, hnum,
  428. saddr, sport);
  429. result = reuseport_select_sock(sk, hash, skb,
  430. sizeof(struct udphdr));
  431. if (result)
  432. return result;
  433. matches = 1;
  434. }
  435. badness = score;
  436. result = sk;
  437. } else if (score == badness && reuseport) {
  438. matches++;
  439. if (reciprocal_scale(hash, matches) == 0)
  440. result = sk;
  441. hash = next_pseudo_random32(hash);
  442. }
  443. }
  444. return result;
  445. }
  446. /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
  447. * harder than this. -DaveM
  448. */
  449. struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
  450. __be16 sport, __be32 daddr, __be16 dport,
  451. int dif, struct udp_table *udptable, struct sk_buff *skb)
  452. {
  453. struct sock *sk, *result;
  454. unsigned short hnum = ntohs(dport);
  455. unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
  456. struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
  457. int score, badness, matches = 0, reuseport = 0;
  458. u32 hash = 0;
  459. if (hslot->count > 10) {
  460. hash2 = udp4_portaddr_hash(net, daddr, hnum);
  461. slot2 = hash2 & udptable->mask;
  462. hslot2 = &udptable->hash2[slot2];
  463. if (hslot->count < hslot2->count)
  464. goto begin;
  465. result = udp4_lib_lookup2(net, saddr, sport,
  466. daddr, hnum, dif,
  467. hslot2, skb);
  468. if (!result) {
  469. unsigned int old_slot2 = slot2;
  470. hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
  471. slot2 = hash2 & udptable->mask;
  472. /* avoid searching the same slot again. */
  473. if (unlikely(slot2 == old_slot2))
  474. return result;
  475. hslot2 = &udptable->hash2[slot2];
  476. if (hslot->count < hslot2->count)
  477. goto begin;
  478. result = udp4_lib_lookup2(net, saddr, sport,
  479. daddr, hnum, dif,
  480. hslot2, skb);
  481. }
  482. return result;
  483. }
  484. begin:
  485. result = NULL;
  486. badness = 0;
  487. sk_for_each_rcu(sk, &hslot->head) {
  488. score = compute_score(sk, net, saddr, sport,
  489. daddr, hnum, dif);
  490. if (score > badness) {
  491. reuseport = sk->sk_reuseport;
  492. if (reuseport) {
  493. hash = udp_ehashfn(net, daddr, hnum,
  494. saddr, sport);
  495. result = reuseport_select_sock(sk, hash, skb,
  496. sizeof(struct udphdr));
  497. if (result)
  498. return result;
  499. matches = 1;
  500. }
  501. result = sk;
  502. badness = score;
  503. } else if (score == badness && reuseport) {
  504. matches++;
  505. if (reciprocal_scale(hash, matches) == 0)
  506. result = sk;
  507. hash = next_pseudo_random32(hash);
  508. }
  509. }
  510. return result;
  511. }
  512. EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
  513. static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
  514. __be16 sport, __be16 dport,
  515. struct udp_table *udptable)
  516. {
  517. const struct iphdr *iph = ip_hdr(skb);
  518. return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
  519. iph->daddr, dport, inet_iif(skb),
  520. udptable, skb);
  521. }
  522. struct sock *udp4_lib_lookup_skb(struct sk_buff *skb,
  523. __be16 sport, __be16 dport)
  524. {
  525. return __udp4_lib_lookup_skb(skb, sport, dport, &udp_table);
  526. }
  527. EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb);
  528. /* Must be called under rcu_read_lock().
  529. * Does increment socket refcount.
  530. */
  531. #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \
  532. IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY)
  533. struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
  534. __be32 daddr, __be16 dport, int dif)
  535. {
  536. struct sock *sk;
  537. sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport,
  538. dif, &udp_table, NULL);
  539. if (sk && !atomic_inc_not_zero(&sk->sk_refcnt))
  540. sk = NULL;
  541. return sk;
  542. }
  543. EXPORT_SYMBOL_GPL(udp4_lib_lookup);
  544. #endif
  545. static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
  546. __be16 loc_port, __be32 loc_addr,
  547. __be16 rmt_port, __be32 rmt_addr,
  548. int dif, unsigned short hnum)
  549. {
  550. struct inet_sock *inet = inet_sk(sk);
  551. if (!net_eq(sock_net(sk), net) ||
  552. udp_sk(sk)->udp_port_hash != hnum ||
  553. (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
  554. (inet->inet_dport != rmt_port && inet->inet_dport) ||
  555. (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
  556. ipv6_only_sock(sk) ||
  557. (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
  558. return false;
  559. if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif))
  560. return false;
  561. return true;
  562. }
  563. /*
  564. * This routine is called by the ICMP module when it gets some
  565. * sort of error condition. If err < 0 then the socket should
  566. * be closed and the error returned to the user. If err > 0
  567. * it's just the icmp type << 8 | icmp code.
  568. * Header points to the ip header of the error packet. We move
  569. * on past this. Then (as it used to claim before adjustment)
  570. * header points to the first 8 bytes of the udp header. We need
  571. * to find the appropriate port.
  572. */
  573. void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
  574. {
  575. struct inet_sock *inet;
  576. const struct iphdr *iph = (const struct iphdr *)skb->data;
  577. struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
  578. const int type = icmp_hdr(skb)->type;
  579. const int code = icmp_hdr(skb)->code;
  580. struct sock *sk;
  581. int harderr;
  582. int err;
  583. struct net *net = dev_net(skb->dev);
  584. sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
  585. iph->saddr, uh->source, skb->dev->ifindex, udptable,
  586. NULL);
  587. if (!sk) {
  588. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  589. return; /* No socket for error */
  590. }
  591. err = 0;
  592. harderr = 0;
  593. inet = inet_sk(sk);
  594. switch (type) {
  595. default:
  596. case ICMP_TIME_EXCEEDED:
  597. err = EHOSTUNREACH;
  598. break;
  599. case ICMP_SOURCE_QUENCH:
  600. goto out;
  601. case ICMP_PARAMETERPROB:
  602. err = EPROTO;
  603. harderr = 1;
  604. break;
  605. case ICMP_DEST_UNREACH:
  606. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  607. ipv4_sk_update_pmtu(skb, sk, info);
  608. if (inet->pmtudisc != IP_PMTUDISC_DONT) {
  609. err = EMSGSIZE;
  610. harderr = 1;
  611. break;
  612. }
  613. goto out;
  614. }
  615. err = EHOSTUNREACH;
  616. if (code <= NR_ICMP_UNREACH) {
  617. harderr = icmp_err_convert[code].fatal;
  618. err = icmp_err_convert[code].errno;
  619. }
  620. break;
  621. case ICMP_REDIRECT:
  622. ipv4_sk_redirect(skb, sk);
  623. goto out;
  624. }
  625. /*
  626. * RFC1122: OK. Passes ICMP errors back to application, as per
  627. * 4.1.3.3.
  628. */
  629. if (!inet->recverr) {
  630. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  631. goto out;
  632. } else
  633. ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
  634. sk->sk_err = err;
  635. sk->sk_error_report(sk);
  636. out:
  637. return;
  638. }
  639. void udp_err(struct sk_buff *skb, u32 info)
  640. {
  641. __udp4_lib_err(skb, info, &udp_table);
  642. }
  643. /*
  644. * Throw away all pending data and cancel the corking. Socket is locked.
  645. */
  646. void udp_flush_pending_frames(struct sock *sk)
  647. {
  648. struct udp_sock *up = udp_sk(sk);
  649. if (up->pending) {
  650. up->len = 0;
  651. up->pending = 0;
  652. ip_flush_pending_frames(sk);
  653. }
  654. }
  655. EXPORT_SYMBOL(udp_flush_pending_frames);
  656. /**
  657. * udp4_hwcsum - handle outgoing HW checksumming
  658. * @skb: sk_buff containing the filled-in UDP header
  659. * (checksum field must be zeroed out)
  660. * @src: source IP address
  661. * @dst: destination IP address
  662. */
  663. void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
  664. {
  665. struct udphdr *uh = udp_hdr(skb);
  666. int offset = skb_transport_offset(skb);
  667. int len = skb->len - offset;
  668. int hlen = len;
  669. __wsum csum = 0;
  670. if (!skb_has_frag_list(skb)) {
  671. /*
  672. * Only one fragment on the socket.
  673. */
  674. skb->csum_start = skb_transport_header(skb) - skb->head;
  675. skb->csum_offset = offsetof(struct udphdr, check);
  676. uh->check = ~csum_tcpudp_magic(src, dst, len,
  677. IPPROTO_UDP, 0);
  678. } else {
  679. struct sk_buff *frags;
  680. /*
  681. * HW-checksum won't work as there are two or more
  682. * fragments on the socket so that all csums of sk_buffs
  683. * should be together
  684. */
  685. skb_walk_frags(skb, frags) {
  686. csum = csum_add(csum, frags->csum);
  687. hlen -= frags->len;
  688. }
  689. csum = skb_checksum(skb, offset, hlen, csum);
  690. skb->ip_summed = CHECKSUM_NONE;
  691. uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
  692. if (uh->check == 0)
  693. uh->check = CSUM_MANGLED_0;
  694. }
  695. }
  696. EXPORT_SYMBOL_GPL(udp4_hwcsum);
  697. /* Function to set UDP checksum for an IPv4 UDP packet. This is intended
  698. * for the simple case like when setting the checksum for a UDP tunnel.
  699. */
  700. void udp_set_csum(bool nocheck, struct sk_buff *skb,
  701. __be32 saddr, __be32 daddr, int len)
  702. {
  703. struct udphdr *uh = udp_hdr(skb);
  704. if (nocheck) {
  705. uh->check = 0;
  706. } else if (skb_is_gso(skb)) {
  707. uh->check = ~udp_v4_check(len, saddr, daddr, 0);
  708. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  709. uh->check = 0;
  710. uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb));
  711. if (uh->check == 0)
  712. uh->check = CSUM_MANGLED_0;
  713. } else {
  714. skb->ip_summed = CHECKSUM_PARTIAL;
  715. skb->csum_start = skb_transport_header(skb) - skb->head;
  716. skb->csum_offset = offsetof(struct udphdr, check);
  717. uh->check = ~udp_v4_check(len, saddr, daddr, 0);
  718. }
  719. }
  720. EXPORT_SYMBOL(udp_set_csum);
  721. static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4)
  722. {
  723. struct sock *sk = skb->sk;
  724. struct inet_sock *inet = inet_sk(sk);
  725. struct udphdr *uh;
  726. int err = 0;
  727. int is_udplite = IS_UDPLITE(sk);
  728. int offset = skb_transport_offset(skb);
  729. int len = skb->len - offset;
  730. __wsum csum = 0;
  731. /*
  732. * Create a UDP header
  733. */
  734. uh = udp_hdr(skb);
  735. uh->source = inet->inet_sport;
  736. uh->dest = fl4->fl4_dport;
  737. uh->len = htons(len);
  738. uh->check = 0;
  739. if (is_udplite) /* UDP-Lite */
  740. csum = udplite_csum(skb);
  741. else if (sk->sk_no_check_tx) { /* UDP csum disabled */
  742. skb->ip_summed = CHECKSUM_NONE;
  743. goto send;
  744. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  745. udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
  746. goto send;
  747. } else
  748. csum = udp_csum(skb);
  749. /* add protocol-dependent pseudo-header */
  750. uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
  751. sk->sk_protocol, csum);
  752. if (uh->check == 0)
  753. uh->check = CSUM_MANGLED_0;
  754. send:
  755. err = ip_send_skb(sock_net(sk), skb);
  756. if (err) {
  757. if (err == -ENOBUFS && !inet->recverr) {
  758. UDP_INC_STATS(sock_net(sk),
  759. UDP_MIB_SNDBUFERRORS, is_udplite);
  760. err = 0;
  761. }
  762. } else
  763. UDP_INC_STATS(sock_net(sk),
  764. UDP_MIB_OUTDATAGRAMS, is_udplite);
  765. return err;
  766. }
  767. /*
  768. * Push out all pending data as one UDP datagram. Socket is locked.
  769. */
  770. int udp_push_pending_frames(struct sock *sk)
  771. {
  772. struct udp_sock *up = udp_sk(sk);
  773. struct inet_sock *inet = inet_sk(sk);
  774. struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
  775. struct sk_buff *skb;
  776. int err = 0;
  777. skb = ip_finish_skb(sk, fl4);
  778. if (!skb)
  779. goto out;
  780. err = udp_send_skb(skb, fl4);
  781. out:
  782. up->len = 0;
  783. up->pending = 0;
  784. return err;
  785. }
  786. EXPORT_SYMBOL(udp_push_pending_frames);
  787. int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  788. {
  789. struct inet_sock *inet = inet_sk(sk);
  790. struct udp_sock *up = udp_sk(sk);
  791. struct flowi4 fl4_stack;
  792. struct flowi4 *fl4;
  793. int ulen = len;
  794. struct ipcm_cookie ipc;
  795. struct rtable *rt = NULL;
  796. int free = 0;
  797. int connected = 0;
  798. __be32 daddr, faddr, saddr;
  799. __be16 dport;
  800. u8 tos;
  801. int err, is_udplite = IS_UDPLITE(sk);
  802. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  803. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  804. struct sk_buff *skb;
  805. struct ip_options_data opt_copy;
  806. if (len > 0xFFFF)
  807. return -EMSGSIZE;
  808. /*
  809. * Check the flags.
  810. */
  811. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
  812. return -EOPNOTSUPP;
  813. ipc.opt = NULL;
  814. ipc.tx_flags = 0;
  815. ipc.ttl = 0;
  816. ipc.tos = -1;
  817. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  818. fl4 = &inet->cork.fl.u.ip4;
  819. if (up->pending) {
  820. /*
  821. * There are pending frames.
  822. * The socket lock must be held while it's corked.
  823. */
  824. lock_sock(sk);
  825. if (likely(up->pending)) {
  826. if (unlikely(up->pending != AF_INET)) {
  827. release_sock(sk);
  828. return -EINVAL;
  829. }
  830. goto do_append_data;
  831. }
  832. release_sock(sk);
  833. }
  834. ulen += sizeof(struct udphdr);
  835. /*
  836. * Get and verify the address.
  837. */
  838. if (msg->msg_name) {
  839. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  840. if (msg->msg_namelen < sizeof(*usin))
  841. return -EINVAL;
  842. if (usin->sin_family != AF_INET) {
  843. if (usin->sin_family != AF_UNSPEC)
  844. return -EAFNOSUPPORT;
  845. }
  846. daddr = usin->sin_addr.s_addr;
  847. dport = usin->sin_port;
  848. if (dport == 0)
  849. return -EINVAL;
  850. } else {
  851. if (sk->sk_state != TCP_ESTABLISHED)
  852. return -EDESTADDRREQ;
  853. daddr = inet->inet_daddr;
  854. dport = inet->inet_dport;
  855. /* Open fast path for connected socket.
  856. Route will not be used, if at least one option is set.
  857. */
  858. connected = 1;
  859. }
  860. ipc.sockc.tsflags = sk->sk_tsflags;
  861. ipc.addr = inet->inet_saddr;
  862. ipc.oif = sk->sk_bound_dev_if;
  863. if (msg->msg_controllen) {
  864. err = ip_cmsg_send(sk, msg, &ipc, sk->sk_family == AF_INET6);
  865. if (unlikely(err)) {
  866. kfree(ipc.opt);
  867. return err;
  868. }
  869. if (ipc.opt)
  870. free = 1;
  871. connected = 0;
  872. }
  873. if (!ipc.opt) {
  874. struct ip_options_rcu *inet_opt;
  875. rcu_read_lock();
  876. inet_opt = rcu_dereference(inet->inet_opt);
  877. if (inet_opt) {
  878. memcpy(&opt_copy, inet_opt,
  879. sizeof(*inet_opt) + inet_opt->opt.optlen);
  880. ipc.opt = &opt_copy.opt;
  881. }
  882. rcu_read_unlock();
  883. }
  884. saddr = ipc.addr;
  885. ipc.addr = faddr = daddr;
  886. sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags);
  887. if (ipc.opt && ipc.opt->opt.srr) {
  888. if (!daddr)
  889. return -EINVAL;
  890. faddr = ipc.opt->opt.faddr;
  891. connected = 0;
  892. }
  893. tos = get_rttos(&ipc, inet);
  894. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  895. (msg->msg_flags & MSG_DONTROUTE) ||
  896. (ipc.opt && ipc.opt->opt.is_strictroute)) {
  897. tos |= RTO_ONLINK;
  898. connected = 0;
  899. }
  900. if (ipv4_is_multicast(daddr)) {
  901. if (!ipc.oif)
  902. ipc.oif = inet->mc_index;
  903. if (!saddr)
  904. saddr = inet->mc_addr;
  905. connected = 0;
  906. } else if (!ipc.oif)
  907. ipc.oif = inet->uc_index;
  908. if (connected)
  909. rt = (struct rtable *)sk_dst_check(sk, 0);
  910. if (!rt) {
  911. struct net *net = sock_net(sk);
  912. __u8 flow_flags = inet_sk_flowi_flags(sk);
  913. fl4 = &fl4_stack;
  914. flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
  915. RT_SCOPE_UNIVERSE, sk->sk_protocol,
  916. flow_flags,
  917. faddr, saddr, dport, inet->inet_sport,
  918. sk->sk_uid);
  919. security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
  920. rt = ip_route_output_flow(net, fl4, sk);
  921. if (IS_ERR(rt)) {
  922. err = PTR_ERR(rt);
  923. rt = NULL;
  924. if (err == -ENETUNREACH)
  925. IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
  926. goto out;
  927. }
  928. err = -EACCES;
  929. if ((rt->rt_flags & RTCF_BROADCAST) &&
  930. !sock_flag(sk, SOCK_BROADCAST))
  931. goto out;
  932. if (connected)
  933. sk_dst_set(sk, dst_clone(&rt->dst));
  934. }
  935. if (msg->msg_flags&MSG_CONFIRM)
  936. goto do_confirm;
  937. back_from_confirm:
  938. saddr = fl4->saddr;
  939. if (!ipc.addr)
  940. daddr = ipc.addr = fl4->daddr;
  941. /* Lockless fast path for the non-corking case. */
  942. if (!corkreq) {
  943. skb = ip_make_skb(sk, fl4, getfrag, msg, ulen,
  944. sizeof(struct udphdr), &ipc, &rt,
  945. msg->msg_flags);
  946. err = PTR_ERR(skb);
  947. if (!IS_ERR_OR_NULL(skb))
  948. err = udp_send_skb(skb, fl4);
  949. goto out;
  950. }
  951. lock_sock(sk);
  952. if (unlikely(up->pending)) {
  953. /* The socket is already corked while preparing it. */
  954. /* ... which is an evident application bug. --ANK */
  955. release_sock(sk);
  956. net_dbg_ratelimited("cork app bug 2\n");
  957. err = -EINVAL;
  958. goto out;
  959. }
  960. /*
  961. * Now cork the socket to pend data.
  962. */
  963. fl4 = &inet->cork.fl.u.ip4;
  964. fl4->daddr = daddr;
  965. fl4->saddr = saddr;
  966. fl4->fl4_dport = dport;
  967. fl4->fl4_sport = inet->inet_sport;
  968. up->pending = AF_INET;
  969. do_append_data:
  970. up->len += ulen;
  971. err = ip_append_data(sk, fl4, getfrag, msg, ulen,
  972. sizeof(struct udphdr), &ipc, &rt,
  973. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  974. if (err)
  975. udp_flush_pending_frames(sk);
  976. else if (!corkreq)
  977. err = udp_push_pending_frames(sk);
  978. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  979. up->pending = 0;
  980. release_sock(sk);
  981. out:
  982. ip_rt_put(rt);
  983. if (free)
  984. kfree(ipc.opt);
  985. if (!err)
  986. return len;
  987. /*
  988. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  989. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  990. * we don't have a good statistic (IpOutDiscards but it can be too many
  991. * things). We could add another new stat but at least for now that
  992. * seems like overkill.
  993. */
  994. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  995. UDP_INC_STATS(sock_net(sk),
  996. UDP_MIB_SNDBUFERRORS, is_udplite);
  997. }
  998. return err;
  999. do_confirm:
  1000. dst_confirm(&rt->dst);
  1001. if (!(msg->msg_flags&MSG_PROBE) || len)
  1002. goto back_from_confirm;
  1003. err = 0;
  1004. goto out;
  1005. }
  1006. EXPORT_SYMBOL(udp_sendmsg);
  1007. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  1008. size_t size, int flags)
  1009. {
  1010. struct inet_sock *inet = inet_sk(sk);
  1011. struct udp_sock *up = udp_sk(sk);
  1012. int ret;
  1013. if (flags & MSG_SENDPAGE_NOTLAST)
  1014. flags |= MSG_MORE;
  1015. if (!up->pending) {
  1016. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  1017. /* Call udp_sendmsg to specify destination address which
  1018. * sendpage interface can't pass.
  1019. * This will succeed only when the socket is connected.
  1020. */
  1021. ret = udp_sendmsg(sk, &msg, 0);
  1022. if (ret < 0)
  1023. return ret;
  1024. }
  1025. lock_sock(sk);
  1026. if (unlikely(!up->pending)) {
  1027. release_sock(sk);
  1028. net_dbg_ratelimited("udp cork app bug 3\n");
  1029. return -EINVAL;
  1030. }
  1031. ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
  1032. page, offset, size, flags);
  1033. if (ret == -EOPNOTSUPP) {
  1034. release_sock(sk);
  1035. return sock_no_sendpage(sk->sk_socket, page, offset,
  1036. size, flags);
  1037. }
  1038. if (ret < 0) {
  1039. udp_flush_pending_frames(sk);
  1040. goto out;
  1041. }
  1042. up->len += size;
  1043. if (!(up->corkflag || (flags&MSG_MORE)))
  1044. ret = udp_push_pending_frames(sk);
  1045. if (!ret)
  1046. ret = size;
  1047. out:
  1048. release_sock(sk);
  1049. return ret;
  1050. }
  1051. static void udp_rmem_release(struct sock *sk, int size, int partial)
  1052. {
  1053. int amt;
  1054. atomic_sub(size, &sk->sk_rmem_alloc);
  1055. spin_lock_bh(&sk->sk_receive_queue.lock);
  1056. sk->sk_forward_alloc += size;
  1057. amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1);
  1058. sk->sk_forward_alloc -= amt;
  1059. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1060. if (amt)
  1061. __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT);
  1062. }
  1063. static void udp_rmem_free(struct sk_buff *skb)
  1064. {
  1065. udp_rmem_release(skb->sk, skb->truesize, 1);
  1066. }
  1067. int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
  1068. {
  1069. struct sk_buff_head *list = &sk->sk_receive_queue;
  1070. int rmem, delta, amt, err = -ENOMEM;
  1071. int size = skb->truesize;
  1072. /* try to avoid the costly atomic add/sub pair when the receive
  1073. * queue is full; always allow at least a packet
  1074. */
  1075. rmem = atomic_read(&sk->sk_rmem_alloc);
  1076. if (rmem && (rmem + size > sk->sk_rcvbuf))
  1077. goto drop;
  1078. /* we drop only if the receive buf is full and the receive
  1079. * queue contains some other skb
  1080. */
  1081. rmem = atomic_add_return(size, &sk->sk_rmem_alloc);
  1082. if ((rmem > sk->sk_rcvbuf) && (rmem > size))
  1083. goto uncharge_drop;
  1084. spin_lock(&list->lock);
  1085. if (size >= sk->sk_forward_alloc) {
  1086. amt = sk_mem_pages(size);
  1087. delta = amt << SK_MEM_QUANTUM_SHIFT;
  1088. if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) {
  1089. err = -ENOBUFS;
  1090. spin_unlock(&list->lock);
  1091. goto uncharge_drop;
  1092. }
  1093. sk->sk_forward_alloc += delta;
  1094. }
  1095. sk->sk_forward_alloc -= size;
  1096. /* the skb owner in now the udp socket */
  1097. skb->sk = sk;
  1098. skb->destructor = udp_rmem_free;
  1099. skb->dev = NULL;
  1100. sock_skb_set_dropcount(sk, skb);
  1101. __skb_queue_tail(list, skb);
  1102. spin_unlock(&list->lock);
  1103. if (!sock_flag(sk, SOCK_DEAD))
  1104. sk->sk_data_ready(sk);
  1105. return 0;
  1106. uncharge_drop:
  1107. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  1108. drop:
  1109. atomic_inc(&sk->sk_drops);
  1110. return err;
  1111. }
  1112. EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb);
  1113. static void udp_destruct_sock(struct sock *sk)
  1114. {
  1115. /* reclaim completely the forward allocated memory */
  1116. __skb_queue_purge(&sk->sk_receive_queue);
  1117. udp_rmem_release(sk, 0, 0);
  1118. inet_sock_destruct(sk);
  1119. }
  1120. int udp_init_sock(struct sock *sk)
  1121. {
  1122. sk->sk_destruct = udp_destruct_sock;
  1123. return 0;
  1124. }
  1125. EXPORT_SYMBOL_GPL(udp_init_sock);
  1126. void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len)
  1127. {
  1128. if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) {
  1129. bool slow = lock_sock_fast(sk);
  1130. sk_peek_offset_bwd(sk, len);
  1131. unlock_sock_fast(sk, slow);
  1132. }
  1133. consume_skb(skb);
  1134. }
  1135. EXPORT_SYMBOL_GPL(skb_consume_udp);
  1136. /**
  1137. * first_packet_length - return length of first packet in receive queue
  1138. * @sk: socket
  1139. *
  1140. * Drops all bad checksum frames, until a valid one is found.
  1141. * Returns the length of found skb, or -1 if none is found.
  1142. */
  1143. static int first_packet_length(struct sock *sk)
  1144. {
  1145. struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
  1146. struct sk_buff *skb;
  1147. int res;
  1148. __skb_queue_head_init(&list_kill);
  1149. spin_lock_bh(&rcvq->lock);
  1150. while ((skb = skb_peek(rcvq)) != NULL &&
  1151. udp_lib_checksum_complete(skb)) {
  1152. __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
  1153. IS_UDPLITE(sk));
  1154. __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
  1155. IS_UDPLITE(sk));
  1156. atomic_inc(&sk->sk_drops);
  1157. __skb_unlink(skb, rcvq);
  1158. __skb_queue_tail(&list_kill, skb);
  1159. }
  1160. res = skb ? skb->len : -1;
  1161. spin_unlock_bh(&rcvq->lock);
  1162. __skb_queue_purge(&list_kill);
  1163. return res;
  1164. }
  1165. /*
  1166. * IOCTL requests applicable to the UDP protocol
  1167. */
  1168. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  1169. {
  1170. switch (cmd) {
  1171. case SIOCOUTQ:
  1172. {
  1173. int amount = sk_wmem_alloc_get(sk);
  1174. return put_user(amount, (int __user *)arg);
  1175. }
  1176. case SIOCINQ:
  1177. {
  1178. int amount = max_t(int, 0, first_packet_length(sk));
  1179. return put_user(amount, (int __user *)arg);
  1180. }
  1181. default:
  1182. return -ENOIOCTLCMD;
  1183. }
  1184. return 0;
  1185. }
  1186. EXPORT_SYMBOL(udp_ioctl);
  1187. /*
  1188. * This should be easy, if there is something there we
  1189. * return it, otherwise we block.
  1190. */
  1191. int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
  1192. int flags, int *addr_len)
  1193. {
  1194. struct inet_sock *inet = inet_sk(sk);
  1195. DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
  1196. struct sk_buff *skb;
  1197. unsigned int ulen, copied;
  1198. int peeked, peeking, off;
  1199. int err;
  1200. int is_udplite = IS_UDPLITE(sk);
  1201. bool checksum_valid = false;
  1202. if (flags & MSG_ERRQUEUE)
  1203. return ip_recv_error(sk, msg, len, addr_len);
  1204. try_again:
  1205. peeking = off = sk_peek_offset(sk, flags);
  1206. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  1207. &peeked, &off, &err);
  1208. if (!skb)
  1209. return err;
  1210. ulen = skb->len;
  1211. copied = len;
  1212. if (copied > ulen - off)
  1213. copied = ulen - off;
  1214. else if (copied < ulen)
  1215. msg->msg_flags |= MSG_TRUNC;
  1216. /*
  1217. * If checksum is needed at all, try to do it while copying the
  1218. * data. If the data is truncated, or if we only want a partial
  1219. * coverage checksum (UDP-Lite), do it before the copy.
  1220. */
  1221. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov || peeking) {
  1222. checksum_valid = !udp_lib_checksum_complete(skb);
  1223. if (!checksum_valid)
  1224. goto csum_copy_err;
  1225. }
  1226. if (checksum_valid || skb_csum_unnecessary(skb))
  1227. err = skb_copy_datagram_msg(skb, off, msg, copied);
  1228. else {
  1229. err = skb_copy_and_csum_datagram_msg(skb, off, msg);
  1230. if (err == -EINVAL)
  1231. goto csum_copy_err;
  1232. }
  1233. if (unlikely(err)) {
  1234. if (!peeked) {
  1235. atomic_inc(&sk->sk_drops);
  1236. UDP_INC_STATS(sock_net(sk),
  1237. UDP_MIB_INERRORS, is_udplite);
  1238. }
  1239. kfree_skb(skb);
  1240. return err;
  1241. }
  1242. if (!peeked)
  1243. UDP_INC_STATS(sock_net(sk),
  1244. UDP_MIB_INDATAGRAMS, is_udplite);
  1245. sock_recv_ts_and_drops(msg, sk, skb);
  1246. /* Copy the address. */
  1247. if (sin) {
  1248. sin->sin_family = AF_INET;
  1249. sin->sin_port = udp_hdr(skb)->source;
  1250. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  1251. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  1252. *addr_len = sizeof(*sin);
  1253. }
  1254. if (inet->cmsg_flags)
  1255. ip_cmsg_recv_offset(msg, skb, sizeof(struct udphdr), off);
  1256. err = copied;
  1257. if (flags & MSG_TRUNC)
  1258. err = ulen;
  1259. skb_consume_udp(sk, skb, peeking ? -err : err);
  1260. return err;
  1261. csum_copy_err:
  1262. if (!__sk_queue_drop_skb(sk, skb, flags)) {
  1263. UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
  1264. UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1265. }
  1266. kfree_skb(skb);
  1267. /* starting over for a new packet, but check if we need to yield */
  1268. cond_resched();
  1269. msg->msg_flags &= ~MSG_TRUNC;
  1270. goto try_again;
  1271. }
  1272. int __udp_disconnect(struct sock *sk, int flags)
  1273. {
  1274. struct inet_sock *inet = inet_sk(sk);
  1275. /*
  1276. * 1003.1g - break association.
  1277. */
  1278. sk->sk_state = TCP_CLOSE;
  1279. inet->inet_daddr = 0;
  1280. inet->inet_dport = 0;
  1281. sock_rps_reset_rxhash(sk);
  1282. sk->sk_bound_dev_if = 0;
  1283. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  1284. inet_reset_saddr(sk);
  1285. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  1286. sk->sk_prot->unhash(sk);
  1287. inet->inet_sport = 0;
  1288. }
  1289. sk_dst_reset(sk);
  1290. return 0;
  1291. }
  1292. EXPORT_SYMBOL(__udp_disconnect);
  1293. int udp_disconnect(struct sock *sk, int flags)
  1294. {
  1295. lock_sock(sk);
  1296. __udp_disconnect(sk, flags);
  1297. release_sock(sk);
  1298. return 0;
  1299. }
  1300. EXPORT_SYMBOL(udp_disconnect);
  1301. void udp_lib_unhash(struct sock *sk)
  1302. {
  1303. if (sk_hashed(sk)) {
  1304. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  1305. struct udp_hslot *hslot, *hslot2;
  1306. hslot = udp_hashslot(udptable, sock_net(sk),
  1307. udp_sk(sk)->udp_port_hash);
  1308. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  1309. spin_lock_bh(&hslot->lock);
  1310. if (rcu_access_pointer(sk->sk_reuseport_cb))
  1311. reuseport_detach_sock(sk);
  1312. if (sk_del_node_init_rcu(sk)) {
  1313. hslot->count--;
  1314. inet_sk(sk)->inet_num = 0;
  1315. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  1316. spin_lock(&hslot2->lock);
  1317. hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
  1318. hslot2->count--;
  1319. spin_unlock(&hslot2->lock);
  1320. }
  1321. spin_unlock_bh(&hslot->lock);
  1322. }
  1323. }
  1324. EXPORT_SYMBOL(udp_lib_unhash);
  1325. /*
  1326. * inet_rcv_saddr was changed, we must rehash secondary hash
  1327. */
  1328. void udp_lib_rehash(struct sock *sk, u16 newhash)
  1329. {
  1330. if (sk_hashed(sk)) {
  1331. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  1332. struct udp_hslot *hslot, *hslot2, *nhslot2;
  1333. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  1334. nhslot2 = udp_hashslot2(udptable, newhash);
  1335. udp_sk(sk)->udp_portaddr_hash = newhash;
  1336. if (hslot2 != nhslot2 ||
  1337. rcu_access_pointer(sk->sk_reuseport_cb)) {
  1338. hslot = udp_hashslot(udptable, sock_net(sk),
  1339. udp_sk(sk)->udp_port_hash);
  1340. /* we must lock primary chain too */
  1341. spin_lock_bh(&hslot->lock);
  1342. if (rcu_access_pointer(sk->sk_reuseport_cb))
  1343. reuseport_detach_sock(sk);
  1344. if (hslot2 != nhslot2) {
  1345. spin_lock(&hslot2->lock);
  1346. hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
  1347. hslot2->count--;
  1348. spin_unlock(&hslot2->lock);
  1349. spin_lock(&nhslot2->lock);
  1350. hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
  1351. &nhslot2->head);
  1352. nhslot2->count++;
  1353. spin_unlock(&nhslot2->lock);
  1354. }
  1355. spin_unlock_bh(&hslot->lock);
  1356. }
  1357. }
  1358. }
  1359. EXPORT_SYMBOL(udp_lib_rehash);
  1360. static void udp_v4_rehash(struct sock *sk)
  1361. {
  1362. u16 new_hash = udp4_portaddr_hash(sock_net(sk),
  1363. inet_sk(sk)->inet_rcv_saddr,
  1364. inet_sk(sk)->inet_num);
  1365. udp_lib_rehash(sk, new_hash);
  1366. }
  1367. static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1368. {
  1369. int rc;
  1370. if (inet_sk(sk)->inet_daddr) {
  1371. sock_rps_save_rxhash(sk, skb);
  1372. sk_mark_napi_id(sk, skb);
  1373. sk_incoming_cpu_update(sk);
  1374. }
  1375. rc = __udp_enqueue_schedule_skb(sk, skb);
  1376. if (rc < 0) {
  1377. int is_udplite = IS_UDPLITE(sk);
  1378. /* Note that an ENOMEM error is charged twice */
  1379. if (rc == -ENOMEM)
  1380. UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  1381. is_udplite);
  1382. UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1383. kfree_skb(skb);
  1384. trace_udp_fail_queue_rcv_skb(rc, sk);
  1385. return -1;
  1386. }
  1387. return 0;
  1388. }
  1389. static struct static_key udp_encap_needed __read_mostly;
  1390. void udp_encap_enable(void)
  1391. {
  1392. if (!static_key_enabled(&udp_encap_needed))
  1393. static_key_slow_inc(&udp_encap_needed);
  1394. }
  1395. EXPORT_SYMBOL(udp_encap_enable);
  1396. /* returns:
  1397. * -1: error
  1398. * 0: success
  1399. * >0: "udp encap" protocol resubmission
  1400. *
  1401. * Note that in the success and error cases, the skb is assumed to
  1402. * have either been requeued or freed.
  1403. */
  1404. int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1405. {
  1406. struct udp_sock *up = udp_sk(sk);
  1407. int is_udplite = IS_UDPLITE(sk);
  1408. /*
  1409. * Charge it to the socket, dropping if the queue is full.
  1410. */
  1411. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  1412. goto drop;
  1413. nf_reset(skb);
  1414. if (static_key_false(&udp_encap_needed) && up->encap_type) {
  1415. int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
  1416. /*
  1417. * This is an encapsulation socket so pass the skb to
  1418. * the socket's udp_encap_rcv() hook. Otherwise, just
  1419. * fall through and pass this up the UDP socket.
  1420. * up->encap_rcv() returns the following value:
  1421. * =0 if skb was successfully passed to the encap
  1422. * handler or was discarded by it.
  1423. * >0 if skb should be passed on to UDP.
  1424. * <0 if skb should be resubmitted as proto -N
  1425. */
  1426. /* if we're overly short, let UDP handle it */
  1427. encap_rcv = ACCESS_ONCE(up->encap_rcv);
  1428. if (encap_rcv) {
  1429. int ret;
  1430. /* Verify checksum before giving to encap */
  1431. if (udp_lib_checksum_complete(skb))
  1432. goto csum_error;
  1433. ret = encap_rcv(sk, skb);
  1434. if (ret <= 0) {
  1435. __UDP_INC_STATS(sock_net(sk),
  1436. UDP_MIB_INDATAGRAMS,
  1437. is_udplite);
  1438. return -ret;
  1439. }
  1440. }
  1441. /* FALLTHROUGH -- it's a UDP Packet */
  1442. }
  1443. /*
  1444. * UDP-Lite specific tests, ignored on UDP sockets
  1445. */
  1446. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  1447. /*
  1448. * MIB statistics other than incrementing the error count are
  1449. * disabled for the following two types of errors: these depend
  1450. * on the application settings, not on the functioning of the
  1451. * protocol stack as such.
  1452. *
  1453. * RFC 3828 here recommends (sec 3.3): "There should also be a
  1454. * way ... to ... at least let the receiving application block
  1455. * delivery of packets with coverage values less than a value
  1456. * provided by the application."
  1457. */
  1458. if (up->pcrlen == 0) { /* full coverage was set */
  1459. net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n",
  1460. UDP_SKB_CB(skb)->cscov, skb->len);
  1461. goto drop;
  1462. }
  1463. /* The next case involves violating the min. coverage requested
  1464. * by the receiver. This is subtle: if receiver wants x and x is
  1465. * greater than the buffersize/MTU then receiver will complain
  1466. * that it wants x while sender emits packets of smaller size y.
  1467. * Therefore the above ...()->partial_cov statement is essential.
  1468. */
  1469. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  1470. net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n",
  1471. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  1472. goto drop;
  1473. }
  1474. }
  1475. if (rcu_access_pointer(sk->sk_filter) &&
  1476. udp_lib_checksum_complete(skb))
  1477. goto csum_error;
  1478. if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
  1479. goto drop;
  1480. udp_csum_pull_header(skb);
  1481. ipv4_pktinfo_prepare(sk, skb);
  1482. return __udp_queue_rcv_skb(sk, skb);
  1483. csum_error:
  1484. __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
  1485. drop:
  1486. __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1487. atomic_inc(&sk->sk_drops);
  1488. kfree_skb(skb);
  1489. return -1;
  1490. }
  1491. /* For TCP sockets, sk_rx_dst is protected by socket lock
  1492. * For UDP, we use xchg() to guard against concurrent changes.
  1493. */
  1494. static void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
  1495. {
  1496. struct dst_entry *old;
  1497. dst_hold(dst);
  1498. old = xchg(&sk->sk_rx_dst, dst);
  1499. dst_release(old);
  1500. }
  1501. /*
  1502. * Multicasts and broadcasts go to each listener.
  1503. *
  1504. * Note: called only from the BH handler context.
  1505. */
  1506. static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  1507. struct udphdr *uh,
  1508. __be32 saddr, __be32 daddr,
  1509. struct udp_table *udptable,
  1510. int proto)
  1511. {
  1512. struct sock *sk, *first = NULL;
  1513. unsigned short hnum = ntohs(uh->dest);
  1514. struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
  1515. unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
  1516. unsigned int offset = offsetof(typeof(*sk), sk_node);
  1517. int dif = skb->dev->ifindex;
  1518. struct hlist_node *node;
  1519. struct sk_buff *nskb;
  1520. if (use_hash2) {
  1521. hash2_any = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum) &
  1522. udp_table.mask;
  1523. hash2 = udp4_portaddr_hash(net, daddr, hnum) & udp_table.mask;
  1524. start_lookup:
  1525. hslot = &udp_table.hash2[hash2];
  1526. offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
  1527. }
  1528. sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
  1529. if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr,
  1530. uh->source, saddr, dif, hnum))
  1531. continue;
  1532. if (!first) {
  1533. first = sk;
  1534. continue;
  1535. }
  1536. nskb = skb_clone(skb, GFP_ATOMIC);
  1537. if (unlikely(!nskb)) {
  1538. atomic_inc(&sk->sk_drops);
  1539. __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
  1540. IS_UDPLITE(sk));
  1541. __UDP_INC_STATS(net, UDP_MIB_INERRORS,
  1542. IS_UDPLITE(sk));
  1543. continue;
  1544. }
  1545. if (udp_queue_rcv_skb(sk, nskb) > 0)
  1546. consume_skb(nskb);
  1547. }
  1548. /* Also lookup *:port if we are using hash2 and haven't done so yet. */
  1549. if (use_hash2 && hash2 != hash2_any) {
  1550. hash2 = hash2_any;
  1551. goto start_lookup;
  1552. }
  1553. if (first) {
  1554. if (udp_queue_rcv_skb(first, skb) > 0)
  1555. consume_skb(skb);
  1556. } else {
  1557. kfree_skb(skb);
  1558. __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
  1559. proto == IPPROTO_UDPLITE);
  1560. }
  1561. return 0;
  1562. }
  1563. /* Initialize UDP checksum. If exited with zero value (success),
  1564. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  1565. * Otherwise, csum completion requires chacksumming packet body,
  1566. * including udp header and folding it to skb->csum.
  1567. */
  1568. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  1569. int proto)
  1570. {
  1571. int err;
  1572. UDP_SKB_CB(skb)->partial_cov = 0;
  1573. UDP_SKB_CB(skb)->cscov = skb->len;
  1574. if (proto == IPPROTO_UDPLITE) {
  1575. err = udplite_checksum_init(skb, uh);
  1576. if (err)
  1577. return err;
  1578. }
  1579. /* Note, we are only interested in != 0 or == 0, thus the
  1580. * force to int.
  1581. */
  1582. return (__force int)skb_checksum_init_zero_check(skb, proto, uh->check,
  1583. inet_compute_pseudo);
  1584. }
  1585. /*
  1586. * All we need to do is get the socket, and then do a checksum.
  1587. */
  1588. int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  1589. int proto)
  1590. {
  1591. struct sock *sk;
  1592. struct udphdr *uh;
  1593. unsigned short ulen;
  1594. struct rtable *rt = skb_rtable(skb);
  1595. __be32 saddr, daddr;
  1596. struct net *net = dev_net(skb->dev);
  1597. /*
  1598. * Validate the packet.
  1599. */
  1600. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1601. goto drop; /* No space for header. */
  1602. uh = udp_hdr(skb);
  1603. ulen = ntohs(uh->len);
  1604. saddr = ip_hdr(skb)->saddr;
  1605. daddr = ip_hdr(skb)->daddr;
  1606. if (ulen > skb->len)
  1607. goto short_packet;
  1608. if (proto == IPPROTO_UDP) {
  1609. /* UDP validates ulen. */
  1610. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1611. goto short_packet;
  1612. uh = udp_hdr(skb);
  1613. }
  1614. if (udp4_csum_init(skb, uh, proto))
  1615. goto csum_error;
  1616. sk = skb_steal_sock(skb);
  1617. if (sk) {
  1618. struct dst_entry *dst = skb_dst(skb);
  1619. int ret;
  1620. if (unlikely(sk->sk_rx_dst != dst))
  1621. udp_sk_rx_dst_set(sk, dst);
  1622. ret = udp_queue_rcv_skb(sk, skb);
  1623. sock_put(sk);
  1624. /* a return value > 0 means to resubmit the input, but
  1625. * it wants the return to be -protocol, or 0
  1626. */
  1627. if (ret > 0)
  1628. return -ret;
  1629. return 0;
  1630. }
  1631. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1632. return __udp4_lib_mcast_deliver(net, skb, uh,
  1633. saddr, daddr, udptable, proto);
  1634. sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  1635. if (sk) {
  1636. int ret;
  1637. if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
  1638. skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
  1639. inet_compute_pseudo);
  1640. ret = udp_queue_rcv_skb(sk, skb);
  1641. /* a return value > 0 means to resubmit the input, but
  1642. * it wants the return to be -protocol, or 0
  1643. */
  1644. if (ret > 0)
  1645. return -ret;
  1646. return 0;
  1647. }
  1648. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1649. goto drop;
  1650. nf_reset(skb);
  1651. /* No socket. Drop packet silently, if checksum is wrong */
  1652. if (udp_lib_checksum_complete(skb))
  1653. goto csum_error;
  1654. __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1655. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1656. /*
  1657. * Hmm. We got an UDP packet to a port to which we
  1658. * don't wanna listen. Ignore it.
  1659. */
  1660. kfree_skb(skb);
  1661. return 0;
  1662. short_packet:
  1663. net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
  1664. proto == IPPROTO_UDPLITE ? "Lite" : "",
  1665. &saddr, ntohs(uh->source),
  1666. ulen, skb->len,
  1667. &daddr, ntohs(uh->dest));
  1668. goto drop;
  1669. csum_error:
  1670. /*
  1671. * RFC1122: OK. Discards the bad packet silently (as far as
  1672. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1673. */
  1674. net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
  1675. proto == IPPROTO_UDPLITE ? "Lite" : "",
  1676. &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
  1677. ulen);
  1678. __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
  1679. drop:
  1680. __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1681. kfree_skb(skb);
  1682. return 0;
  1683. }
  1684. /* We can only early demux multicast if there is a single matching socket.
  1685. * If more than one socket found returns NULL
  1686. */
  1687. static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
  1688. __be16 loc_port, __be32 loc_addr,
  1689. __be16 rmt_port, __be32 rmt_addr,
  1690. int dif)
  1691. {
  1692. struct sock *sk, *result;
  1693. unsigned short hnum = ntohs(loc_port);
  1694. unsigned int slot = udp_hashfn(net, hnum, udp_table.mask);
  1695. struct udp_hslot *hslot = &udp_table.hash[slot];
  1696. /* Do not bother scanning a too big list */
  1697. if (hslot->count > 10)
  1698. return NULL;
  1699. result = NULL;
  1700. sk_for_each_rcu(sk, &hslot->head) {
  1701. if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr,
  1702. rmt_port, rmt_addr, dif, hnum)) {
  1703. if (result)
  1704. return NULL;
  1705. result = sk;
  1706. }
  1707. }
  1708. return result;
  1709. }
  1710. /* For unicast we should only early demux connected sockets or we can
  1711. * break forwarding setups. The chains here can be long so only check
  1712. * if the first socket is an exact match and if not move on.
  1713. */
  1714. static struct sock *__udp4_lib_demux_lookup(struct net *net,
  1715. __be16 loc_port, __be32 loc_addr,
  1716. __be16 rmt_port, __be32 rmt_addr,
  1717. int dif)
  1718. {
  1719. unsigned short hnum = ntohs(loc_port);
  1720. unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum);
  1721. unsigned int slot2 = hash2 & udp_table.mask;
  1722. struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
  1723. INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
  1724. const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
  1725. struct sock *sk;
  1726. udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
  1727. if (INET_MATCH(sk, net, acookie, rmt_addr,
  1728. loc_addr, ports, dif))
  1729. return sk;
  1730. /* Only check first socket in chain */
  1731. break;
  1732. }
  1733. return NULL;
  1734. }
  1735. void udp_v4_early_demux(struct sk_buff *skb)
  1736. {
  1737. struct net *net = dev_net(skb->dev);
  1738. const struct iphdr *iph;
  1739. const struct udphdr *uh;
  1740. struct sock *sk = NULL;
  1741. struct dst_entry *dst;
  1742. int dif = skb->dev->ifindex;
  1743. int ours;
  1744. /* validate the packet */
  1745. if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
  1746. return;
  1747. iph = ip_hdr(skb);
  1748. uh = udp_hdr(skb);
  1749. if (skb->pkt_type == PACKET_BROADCAST ||
  1750. skb->pkt_type == PACKET_MULTICAST) {
  1751. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  1752. if (!in_dev)
  1753. return;
  1754. /* we are supposed to accept bcast packets */
  1755. if (skb->pkt_type == PACKET_MULTICAST) {
  1756. ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr,
  1757. iph->protocol);
  1758. if (!ours)
  1759. return;
  1760. }
  1761. sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
  1762. uh->source, iph->saddr, dif);
  1763. } else if (skb->pkt_type == PACKET_HOST) {
  1764. sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
  1765. uh->source, iph->saddr, dif);
  1766. }
  1767. if (!sk || !atomic_inc_not_zero_hint(&sk->sk_refcnt, 2))
  1768. return;
  1769. skb->sk = sk;
  1770. skb->destructor = sock_efree;
  1771. dst = READ_ONCE(sk->sk_rx_dst);
  1772. if (dst)
  1773. dst = dst_check(dst, 0);
  1774. if (dst) {
  1775. /* DST_NOCACHE can not be used without taking a reference */
  1776. if (dst->flags & DST_NOCACHE) {
  1777. if (likely(atomic_inc_not_zero(&dst->__refcnt)))
  1778. skb_dst_set(skb, dst);
  1779. } else {
  1780. skb_dst_set_noref(skb, dst);
  1781. }
  1782. }
  1783. }
  1784. int udp_rcv(struct sk_buff *skb)
  1785. {
  1786. return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  1787. }
  1788. void udp_destroy_sock(struct sock *sk)
  1789. {
  1790. struct udp_sock *up = udp_sk(sk);
  1791. bool slow = lock_sock_fast(sk);
  1792. udp_flush_pending_frames(sk);
  1793. unlock_sock_fast(sk, slow);
  1794. if (static_key_false(&udp_encap_needed) && up->encap_type) {
  1795. void (*encap_destroy)(struct sock *sk);
  1796. encap_destroy = ACCESS_ONCE(up->encap_destroy);
  1797. if (encap_destroy)
  1798. encap_destroy(sk);
  1799. }
  1800. }
  1801. /*
  1802. * Socket option code for UDP
  1803. */
  1804. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1805. char __user *optval, unsigned int optlen,
  1806. int (*push_pending_frames)(struct sock *))
  1807. {
  1808. struct udp_sock *up = udp_sk(sk);
  1809. int val, valbool;
  1810. int err = 0;
  1811. int is_udplite = IS_UDPLITE(sk);
  1812. if (optlen < sizeof(int))
  1813. return -EINVAL;
  1814. if (get_user(val, (int __user *)optval))
  1815. return -EFAULT;
  1816. valbool = val ? 1 : 0;
  1817. switch (optname) {
  1818. case UDP_CORK:
  1819. if (val != 0) {
  1820. up->corkflag = 1;
  1821. } else {
  1822. up->corkflag = 0;
  1823. lock_sock(sk);
  1824. push_pending_frames(sk);
  1825. release_sock(sk);
  1826. }
  1827. break;
  1828. case UDP_ENCAP:
  1829. switch (val) {
  1830. case 0:
  1831. case UDP_ENCAP_ESPINUDP:
  1832. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1833. up->encap_rcv = xfrm4_udp_encap_rcv;
  1834. /* FALLTHROUGH */
  1835. case UDP_ENCAP_L2TPINUDP:
  1836. up->encap_type = val;
  1837. udp_encap_enable();
  1838. break;
  1839. default:
  1840. err = -ENOPROTOOPT;
  1841. break;
  1842. }
  1843. break;
  1844. case UDP_NO_CHECK6_TX:
  1845. up->no_check6_tx = valbool;
  1846. break;
  1847. case UDP_NO_CHECK6_RX:
  1848. up->no_check6_rx = valbool;
  1849. break;
  1850. /*
  1851. * UDP-Lite's partial checksum coverage (RFC 3828).
  1852. */
  1853. /* The sender sets actual checksum coverage length via this option.
  1854. * The case coverage > packet length is handled by send module. */
  1855. case UDPLITE_SEND_CSCOV:
  1856. if (!is_udplite) /* Disable the option on UDP sockets */
  1857. return -ENOPROTOOPT;
  1858. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1859. val = 8;
  1860. else if (val > USHRT_MAX)
  1861. val = USHRT_MAX;
  1862. up->pcslen = val;
  1863. up->pcflag |= UDPLITE_SEND_CC;
  1864. break;
  1865. /* The receiver specifies a minimum checksum coverage value. To make
  1866. * sense, this should be set to at least 8 (as done below). If zero is
  1867. * used, this again means full checksum coverage. */
  1868. case UDPLITE_RECV_CSCOV:
  1869. if (!is_udplite) /* Disable the option on UDP sockets */
  1870. return -ENOPROTOOPT;
  1871. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1872. val = 8;
  1873. else if (val > USHRT_MAX)
  1874. val = USHRT_MAX;
  1875. up->pcrlen = val;
  1876. up->pcflag |= UDPLITE_RECV_CC;
  1877. break;
  1878. default:
  1879. err = -ENOPROTOOPT;
  1880. break;
  1881. }
  1882. return err;
  1883. }
  1884. EXPORT_SYMBOL(udp_lib_setsockopt);
  1885. int udp_setsockopt(struct sock *sk, int level, int optname,
  1886. char __user *optval, unsigned int optlen)
  1887. {
  1888. if (level == SOL_UDP || level == SOL_UDPLITE)
  1889. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1890. udp_push_pending_frames);
  1891. return ip_setsockopt(sk, level, optname, optval, optlen);
  1892. }
  1893. #ifdef CONFIG_COMPAT
  1894. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1895. char __user *optval, unsigned int optlen)
  1896. {
  1897. if (level == SOL_UDP || level == SOL_UDPLITE)
  1898. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1899. udp_push_pending_frames);
  1900. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1901. }
  1902. #endif
  1903. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1904. char __user *optval, int __user *optlen)
  1905. {
  1906. struct udp_sock *up = udp_sk(sk);
  1907. int val, len;
  1908. if (get_user(len, optlen))
  1909. return -EFAULT;
  1910. len = min_t(unsigned int, len, sizeof(int));
  1911. if (len < 0)
  1912. return -EINVAL;
  1913. switch (optname) {
  1914. case UDP_CORK:
  1915. val = up->corkflag;
  1916. break;
  1917. case UDP_ENCAP:
  1918. val = up->encap_type;
  1919. break;
  1920. case UDP_NO_CHECK6_TX:
  1921. val = up->no_check6_tx;
  1922. break;
  1923. case UDP_NO_CHECK6_RX:
  1924. val = up->no_check6_rx;
  1925. break;
  1926. /* The following two cannot be changed on UDP sockets, the return is
  1927. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1928. case UDPLITE_SEND_CSCOV:
  1929. val = up->pcslen;
  1930. break;
  1931. case UDPLITE_RECV_CSCOV:
  1932. val = up->pcrlen;
  1933. break;
  1934. default:
  1935. return -ENOPROTOOPT;
  1936. }
  1937. if (put_user(len, optlen))
  1938. return -EFAULT;
  1939. if (copy_to_user(optval, &val, len))
  1940. return -EFAULT;
  1941. return 0;
  1942. }
  1943. EXPORT_SYMBOL(udp_lib_getsockopt);
  1944. int udp_getsockopt(struct sock *sk, int level, int optname,
  1945. char __user *optval, int __user *optlen)
  1946. {
  1947. if (level == SOL_UDP || level == SOL_UDPLITE)
  1948. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1949. return ip_getsockopt(sk, level, optname, optval, optlen);
  1950. }
  1951. #ifdef CONFIG_COMPAT
  1952. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1953. char __user *optval, int __user *optlen)
  1954. {
  1955. if (level == SOL_UDP || level == SOL_UDPLITE)
  1956. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1957. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1958. }
  1959. #endif
  1960. /**
  1961. * udp_poll - wait for a UDP event.
  1962. * @file - file struct
  1963. * @sock - socket
  1964. * @wait - poll table
  1965. *
  1966. * This is same as datagram poll, except for the special case of
  1967. * blocking sockets. If application is using a blocking fd
  1968. * and a packet with checksum error is in the queue;
  1969. * then it could get return from select indicating data available
  1970. * but then block when reading it. Add special case code
  1971. * to work around these arguably broken applications.
  1972. */
  1973. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1974. {
  1975. unsigned int mask = datagram_poll(file, sock, wait);
  1976. struct sock *sk = sock->sk;
  1977. sock_rps_record_flow(sk);
  1978. /* Check for false positives due to checksum errors */
  1979. if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
  1980. !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1)
  1981. mask &= ~(POLLIN | POLLRDNORM);
  1982. return mask;
  1983. }
  1984. EXPORT_SYMBOL(udp_poll);
  1985. int udp_abort(struct sock *sk, int err)
  1986. {
  1987. lock_sock(sk);
  1988. sk->sk_err = err;
  1989. sk->sk_error_report(sk);
  1990. __udp_disconnect(sk, 0);
  1991. release_sock(sk);
  1992. return 0;
  1993. }
  1994. EXPORT_SYMBOL_GPL(udp_abort);
  1995. struct proto udp_prot = {
  1996. .name = "UDP",
  1997. .owner = THIS_MODULE,
  1998. .close = udp_lib_close,
  1999. .connect = ip4_datagram_connect,
  2000. .disconnect = udp_disconnect,
  2001. .ioctl = udp_ioctl,
  2002. .init = udp_init_sock,
  2003. .destroy = udp_destroy_sock,
  2004. .setsockopt = udp_setsockopt,
  2005. .getsockopt = udp_getsockopt,
  2006. .sendmsg = udp_sendmsg,
  2007. .recvmsg = udp_recvmsg,
  2008. .sendpage = udp_sendpage,
  2009. .release_cb = ip4_datagram_release_cb,
  2010. .hash = udp_lib_hash,
  2011. .unhash = udp_lib_unhash,
  2012. .rehash = udp_v4_rehash,
  2013. .get_port = udp_v4_get_port,
  2014. .memory_allocated = &udp_memory_allocated,
  2015. .sysctl_mem = sysctl_udp_mem,
  2016. .sysctl_wmem = &sysctl_udp_wmem_min,
  2017. .sysctl_rmem = &sysctl_udp_rmem_min,
  2018. .obj_size = sizeof(struct udp_sock),
  2019. .h.udp_table = &udp_table,
  2020. #ifdef CONFIG_COMPAT
  2021. .compat_setsockopt = compat_udp_setsockopt,
  2022. .compat_getsockopt = compat_udp_getsockopt,
  2023. #endif
  2024. .diag_destroy = udp_abort,
  2025. };
  2026. EXPORT_SYMBOL(udp_prot);
  2027. /* ------------------------------------------------------------------------ */
  2028. #ifdef CONFIG_PROC_FS
  2029. static struct sock *udp_get_first(struct seq_file *seq, int start)
  2030. {
  2031. struct sock *sk;
  2032. struct udp_iter_state *state = seq->private;
  2033. struct net *net = seq_file_net(seq);
  2034. for (state->bucket = start; state->bucket <= state->udp_table->mask;
  2035. ++state->bucket) {
  2036. struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
  2037. if (hlist_empty(&hslot->head))
  2038. continue;
  2039. spin_lock_bh(&hslot->lock);
  2040. sk_for_each(sk, &hslot->head) {
  2041. if (!net_eq(sock_net(sk), net))
  2042. continue;
  2043. if (sk->sk_family == state->family)
  2044. goto found;
  2045. }
  2046. spin_unlock_bh(&hslot->lock);
  2047. }
  2048. sk = NULL;
  2049. found:
  2050. return sk;
  2051. }
  2052. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  2053. {
  2054. struct udp_iter_state *state = seq->private;
  2055. struct net *net = seq_file_net(seq);
  2056. do {
  2057. sk = sk_next(sk);
  2058. } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
  2059. if (!sk) {
  2060. if (state->bucket <= state->udp_table->mask)
  2061. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  2062. return udp_get_first(seq, state->bucket + 1);
  2063. }
  2064. return sk;
  2065. }
  2066. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  2067. {
  2068. struct sock *sk = udp_get_first(seq, 0);
  2069. if (sk)
  2070. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  2071. --pos;
  2072. return pos ? NULL : sk;
  2073. }
  2074. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  2075. {
  2076. struct udp_iter_state *state = seq->private;
  2077. state->bucket = MAX_UDP_PORTS;
  2078. return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  2079. }
  2080. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2081. {
  2082. struct sock *sk;
  2083. if (v == SEQ_START_TOKEN)
  2084. sk = udp_get_idx(seq, 0);
  2085. else
  2086. sk = udp_get_next(seq, v);
  2087. ++*pos;
  2088. return sk;
  2089. }
  2090. static void udp_seq_stop(struct seq_file *seq, void *v)
  2091. {
  2092. struct udp_iter_state *state = seq->private;
  2093. if (state->bucket <= state->udp_table->mask)
  2094. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  2095. }
  2096. int udp_seq_open(struct inode *inode, struct file *file)
  2097. {
  2098. struct udp_seq_afinfo *afinfo = PDE_DATA(inode);
  2099. struct udp_iter_state *s;
  2100. int err;
  2101. err = seq_open_net(inode, file, &afinfo->seq_ops,
  2102. sizeof(struct udp_iter_state));
  2103. if (err < 0)
  2104. return err;
  2105. s = ((struct seq_file *)file->private_data)->private;
  2106. s->family = afinfo->family;
  2107. s->udp_table = afinfo->udp_table;
  2108. return err;
  2109. }
  2110. EXPORT_SYMBOL(udp_seq_open);
  2111. /* ------------------------------------------------------------------------ */
  2112. int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
  2113. {
  2114. struct proc_dir_entry *p;
  2115. int rc = 0;
  2116. afinfo->seq_ops.start = udp_seq_start;
  2117. afinfo->seq_ops.next = udp_seq_next;
  2118. afinfo->seq_ops.stop = udp_seq_stop;
  2119. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  2120. afinfo->seq_fops, afinfo);
  2121. if (!p)
  2122. rc = -ENOMEM;
  2123. return rc;
  2124. }
  2125. EXPORT_SYMBOL(udp_proc_register);
  2126. void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
  2127. {
  2128. remove_proc_entry(afinfo->name, net->proc_net);
  2129. }
  2130. EXPORT_SYMBOL(udp_proc_unregister);
  2131. /* ------------------------------------------------------------------------ */
  2132. static void udp4_format_sock(struct sock *sp, struct seq_file *f,
  2133. int bucket)
  2134. {
  2135. struct inet_sock *inet = inet_sk(sp);
  2136. __be32 dest = inet->inet_daddr;
  2137. __be32 src = inet->inet_rcv_saddr;
  2138. __u16 destp = ntohs(inet->inet_dport);
  2139. __u16 srcp = ntohs(inet->inet_sport);
  2140. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  2141. " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
  2142. bucket, src, srcp, dest, destp, sp->sk_state,
  2143. sk_wmem_alloc_get(sp),
  2144. sk_rmem_alloc_get(sp),
  2145. 0, 0L, 0,
  2146. from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
  2147. 0, sock_i_ino(sp),
  2148. atomic_read(&sp->sk_refcnt), sp,
  2149. atomic_read(&sp->sk_drops));
  2150. }
  2151. int udp4_seq_show(struct seq_file *seq, void *v)
  2152. {
  2153. seq_setwidth(seq, 127);
  2154. if (v == SEQ_START_TOKEN)
  2155. seq_puts(seq, " sl local_address rem_address st tx_queue "
  2156. "rx_queue tr tm->when retrnsmt uid timeout "
  2157. "inode ref pointer drops");
  2158. else {
  2159. struct udp_iter_state *state = seq->private;
  2160. udp4_format_sock(v, seq, state->bucket);
  2161. }
  2162. seq_pad(seq, '\n');
  2163. return 0;
  2164. }
  2165. static const struct file_operations udp_afinfo_seq_fops = {
  2166. .owner = THIS_MODULE,
  2167. .open = udp_seq_open,
  2168. .read = seq_read,
  2169. .llseek = seq_lseek,
  2170. .release = seq_release_net
  2171. };
  2172. /* ------------------------------------------------------------------------ */
  2173. static struct udp_seq_afinfo udp4_seq_afinfo = {
  2174. .name = "udp",
  2175. .family = AF_INET,
  2176. .udp_table = &udp_table,
  2177. .seq_fops = &udp_afinfo_seq_fops,
  2178. .seq_ops = {
  2179. .show = udp4_seq_show,
  2180. },
  2181. };
  2182. static int __net_init udp4_proc_init_net(struct net *net)
  2183. {
  2184. return udp_proc_register(net, &udp4_seq_afinfo);
  2185. }
  2186. static void __net_exit udp4_proc_exit_net(struct net *net)
  2187. {
  2188. udp_proc_unregister(net, &udp4_seq_afinfo);
  2189. }
  2190. static struct pernet_operations udp4_net_ops = {
  2191. .init = udp4_proc_init_net,
  2192. .exit = udp4_proc_exit_net,
  2193. };
  2194. int __init udp4_proc_init(void)
  2195. {
  2196. return register_pernet_subsys(&udp4_net_ops);
  2197. }
  2198. void udp4_proc_exit(void)
  2199. {
  2200. unregister_pernet_subsys(&udp4_net_ops);
  2201. }
  2202. #endif /* CONFIG_PROC_FS */
  2203. static __initdata unsigned long uhash_entries;
  2204. static int __init set_uhash_entries(char *str)
  2205. {
  2206. ssize_t ret;
  2207. if (!str)
  2208. return 0;
  2209. ret = kstrtoul(str, 0, &uhash_entries);
  2210. if (ret)
  2211. return 0;
  2212. if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
  2213. uhash_entries = UDP_HTABLE_SIZE_MIN;
  2214. return 1;
  2215. }
  2216. __setup("uhash_entries=", set_uhash_entries);
  2217. void __init udp_table_init(struct udp_table *table, const char *name)
  2218. {
  2219. unsigned int i;
  2220. table->hash = alloc_large_system_hash(name,
  2221. 2 * sizeof(struct udp_hslot),
  2222. uhash_entries,
  2223. 21, /* one slot per 2 MB */
  2224. 0,
  2225. &table->log,
  2226. &table->mask,
  2227. UDP_HTABLE_SIZE_MIN,
  2228. 64 * 1024);
  2229. table->hash2 = table->hash + (table->mask + 1);
  2230. for (i = 0; i <= table->mask; i++) {
  2231. INIT_HLIST_HEAD(&table->hash[i].head);
  2232. table->hash[i].count = 0;
  2233. spin_lock_init(&table->hash[i].lock);
  2234. }
  2235. for (i = 0; i <= table->mask; i++) {
  2236. INIT_HLIST_HEAD(&table->hash2[i].head);
  2237. table->hash2[i].count = 0;
  2238. spin_lock_init(&table->hash2[i].lock);
  2239. }
  2240. }
  2241. u32 udp_flow_hashrnd(void)
  2242. {
  2243. static u32 hashrnd __read_mostly;
  2244. net_get_random_once(&hashrnd, sizeof(hashrnd));
  2245. return hashrnd;
  2246. }
  2247. EXPORT_SYMBOL(udp_flow_hashrnd);
  2248. void __init udp_init(void)
  2249. {
  2250. unsigned long limit;
  2251. udp_table_init(&udp_table, "UDP");
  2252. limit = nr_free_buffer_pages() / 8;
  2253. limit = max(limit, 128UL);
  2254. sysctl_udp_mem[0] = limit / 4 * 3;
  2255. sysctl_udp_mem[1] = limit;
  2256. sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
  2257. sysctl_udp_rmem_min = SK_MEM_QUANTUM;
  2258. sysctl_udp_wmem_min = SK_MEM_QUANTUM;
  2259. }