udp.c 57 KB

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