udp.c 55 KB

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