udp.c 57 KB

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