udp.c 64 KB

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