udp.c 57 KB

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