udp.c 65 KB

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