udp.c 64 KB

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