udp.c 65 KB

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