udp.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  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. int 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_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-dependant 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(&result->sk_refcnt)))
  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(&result->sk_refcnt)))
  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. struct iphdr *iph = (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. }
  586. sk->sk_err = err;
  587. sk->sk_error_report(sk);
  588. out:
  589. sock_put(sk);
  590. }
  591. void udp_err(struct sk_buff *skb, u32 info)
  592. {
  593. __udp4_lib_err(skb, info, &udp_table);
  594. }
  595. /*
  596. * Throw away all pending data and cancel the corking. Socket is locked.
  597. */
  598. void udp_flush_pending_frames(struct sock *sk)
  599. {
  600. struct udp_sock *up = udp_sk(sk);
  601. if (up->pending) {
  602. up->len = 0;
  603. up->pending = 0;
  604. ip_flush_pending_frames(sk);
  605. }
  606. }
  607. EXPORT_SYMBOL(udp_flush_pending_frames);
  608. /**
  609. * udp4_hwcsum_outgoing - handle outgoing HW checksumming
  610. * @sk: socket we are sending on
  611. * @skb: sk_buff containing the filled-in UDP header
  612. * (checksum field must be zeroed out)
  613. */
  614. static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  615. __be32 src, __be32 dst, int len)
  616. {
  617. unsigned int offset;
  618. struct udphdr *uh = udp_hdr(skb);
  619. __wsum csum = 0;
  620. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  621. /*
  622. * Only one fragment on the socket.
  623. */
  624. skb->csum_start = skb_transport_header(skb) - skb->head;
  625. skb->csum_offset = offsetof(struct udphdr, check);
  626. uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
  627. } else {
  628. /*
  629. * HW-checksum won't work as there are two or more
  630. * fragments on the socket so that all csums of sk_buffs
  631. * should be together
  632. */
  633. offset = skb_transport_offset(skb);
  634. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  635. skb->ip_summed = CHECKSUM_NONE;
  636. skb_queue_walk(&sk->sk_write_queue, skb) {
  637. csum = csum_add(csum, skb->csum);
  638. }
  639. uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
  640. if (uh->check == 0)
  641. uh->check = CSUM_MANGLED_0;
  642. }
  643. }
  644. /*
  645. * Push out all pending data as one UDP datagram. Socket is locked.
  646. */
  647. static int udp_push_pending_frames(struct sock *sk)
  648. {
  649. struct udp_sock *up = udp_sk(sk);
  650. struct inet_sock *inet = inet_sk(sk);
  651. struct flowi *fl = &inet->cork.fl;
  652. struct sk_buff *skb;
  653. struct udphdr *uh;
  654. int err = 0;
  655. int is_udplite = IS_UDPLITE(sk);
  656. __wsum csum = 0;
  657. /* Grab the skbuff where UDP header space exists. */
  658. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  659. goto out;
  660. /*
  661. * Create a UDP header
  662. */
  663. uh = udp_hdr(skb);
  664. uh->source = fl->fl_ip_sport;
  665. uh->dest = fl->fl_ip_dport;
  666. uh->len = htons(up->len);
  667. uh->check = 0;
  668. if (is_udplite) /* UDP-Lite */
  669. csum = udplite_csum_outgoing(sk, skb);
  670. else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
  671. skb->ip_summed = CHECKSUM_NONE;
  672. goto send;
  673. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  674. udp4_hwcsum_outgoing(sk, skb, fl->fl4_src, fl->fl4_dst, up->len);
  675. goto send;
  676. } else /* `normal' UDP */
  677. csum = udp_csum_outgoing(sk, skb);
  678. /* add protocol-dependent pseudo-header */
  679. uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
  680. sk->sk_protocol, csum);
  681. if (uh->check == 0)
  682. uh->check = CSUM_MANGLED_0;
  683. send:
  684. err = ip_push_pending_frames(sk);
  685. if (err) {
  686. if (err == -ENOBUFS && !inet->recverr) {
  687. UDP_INC_STATS_USER(sock_net(sk),
  688. UDP_MIB_SNDBUFERRORS, is_udplite);
  689. err = 0;
  690. }
  691. } else
  692. UDP_INC_STATS_USER(sock_net(sk),
  693. UDP_MIB_OUTDATAGRAMS, is_udplite);
  694. out:
  695. up->len = 0;
  696. up->pending = 0;
  697. return err;
  698. }
  699. int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  700. size_t len)
  701. {
  702. struct inet_sock *inet = inet_sk(sk);
  703. struct udp_sock *up = udp_sk(sk);
  704. int ulen = len;
  705. struct ipcm_cookie ipc;
  706. struct rtable *rt = NULL;
  707. int free = 0;
  708. int connected = 0;
  709. __be32 daddr, faddr, saddr;
  710. __be16 dport;
  711. u8 tos;
  712. int err, is_udplite = IS_UDPLITE(sk);
  713. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  714. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  715. if (len > 0xFFFF)
  716. return -EMSGSIZE;
  717. /*
  718. * Check the flags.
  719. */
  720. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
  721. return -EOPNOTSUPP;
  722. ipc.opt = NULL;
  723. ipc.shtx.flags = 0;
  724. if (up->pending) {
  725. /*
  726. * There are pending frames.
  727. * The socket lock must be held while it's corked.
  728. */
  729. lock_sock(sk);
  730. if (likely(up->pending)) {
  731. if (unlikely(up->pending != AF_INET)) {
  732. release_sock(sk);
  733. return -EINVAL;
  734. }
  735. goto do_append_data;
  736. }
  737. release_sock(sk);
  738. }
  739. ulen += sizeof(struct udphdr);
  740. /*
  741. * Get and verify the address.
  742. */
  743. if (msg->msg_name) {
  744. struct sockaddr_in * usin = (struct sockaddr_in *)msg->msg_name;
  745. if (msg->msg_namelen < sizeof(*usin))
  746. return -EINVAL;
  747. if (usin->sin_family != AF_INET) {
  748. if (usin->sin_family != AF_UNSPEC)
  749. return -EAFNOSUPPORT;
  750. }
  751. daddr = usin->sin_addr.s_addr;
  752. dport = usin->sin_port;
  753. if (dport == 0)
  754. return -EINVAL;
  755. } else {
  756. if (sk->sk_state != TCP_ESTABLISHED)
  757. return -EDESTADDRREQ;
  758. daddr = inet->inet_daddr;
  759. dport = inet->inet_dport;
  760. /* Open fast path for connected socket.
  761. Route will not be used, if at least one option is set.
  762. */
  763. connected = 1;
  764. }
  765. ipc.addr = inet->inet_saddr;
  766. ipc.oif = sk->sk_bound_dev_if;
  767. err = sock_tx_timestamp(msg, sk, &ipc.shtx);
  768. if (err)
  769. return err;
  770. if (msg->msg_controllen) {
  771. err = ip_cmsg_send(sock_net(sk), msg, &ipc);
  772. if (err)
  773. return err;
  774. if (ipc.opt)
  775. free = 1;
  776. connected = 0;
  777. }
  778. if (!ipc.opt)
  779. ipc.opt = inet->opt;
  780. saddr = ipc.addr;
  781. ipc.addr = faddr = daddr;
  782. if (ipc.opt && ipc.opt->srr) {
  783. if (!daddr)
  784. return -EINVAL;
  785. faddr = ipc.opt->faddr;
  786. connected = 0;
  787. }
  788. tos = RT_TOS(inet->tos);
  789. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  790. (msg->msg_flags & MSG_DONTROUTE) ||
  791. (ipc.opt && ipc.opt->is_strictroute)) {
  792. tos |= RTO_ONLINK;
  793. connected = 0;
  794. }
  795. if (ipv4_is_multicast(daddr)) {
  796. if (!ipc.oif)
  797. ipc.oif = inet->mc_index;
  798. if (!saddr)
  799. saddr = inet->mc_addr;
  800. connected = 0;
  801. }
  802. if (connected)
  803. rt = (struct rtable *)sk_dst_check(sk, 0);
  804. if (rt == NULL) {
  805. struct flowi fl = { .oif = ipc.oif,
  806. .mark = sk->sk_mark,
  807. .nl_u = { .ip4_u =
  808. { .daddr = faddr,
  809. .saddr = saddr,
  810. .tos = tos } },
  811. .proto = sk->sk_protocol,
  812. .flags = inet_sk_flowi_flags(sk),
  813. .uli_u = { .ports =
  814. { .sport = inet->inet_sport,
  815. .dport = dport } } };
  816. struct net *net = sock_net(sk);
  817. security_sk_classify_flow(sk, &fl);
  818. err = ip_route_output_flow(net, &rt, &fl, sk, 1);
  819. if (err) {
  820. if (err == -ENETUNREACH)
  821. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  822. goto out;
  823. }
  824. err = -EACCES;
  825. if ((rt->rt_flags & RTCF_BROADCAST) &&
  826. !sock_flag(sk, SOCK_BROADCAST))
  827. goto out;
  828. if (connected)
  829. sk_dst_set(sk, dst_clone(&rt->u.dst));
  830. }
  831. if (msg->msg_flags&MSG_CONFIRM)
  832. goto do_confirm;
  833. back_from_confirm:
  834. saddr = rt->rt_src;
  835. if (!ipc.addr)
  836. daddr = ipc.addr = rt->rt_dst;
  837. lock_sock(sk);
  838. if (unlikely(up->pending)) {
  839. /* The socket is already corked while preparing it. */
  840. /* ... which is an evident application bug. --ANK */
  841. release_sock(sk);
  842. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  843. err = -EINVAL;
  844. goto out;
  845. }
  846. /*
  847. * Now cork the socket to pend data.
  848. */
  849. inet->cork.fl.fl4_dst = daddr;
  850. inet->cork.fl.fl_ip_dport = dport;
  851. inet->cork.fl.fl4_src = saddr;
  852. inet->cork.fl.fl_ip_sport = inet->inet_sport;
  853. up->pending = AF_INET;
  854. do_append_data:
  855. up->len += ulen;
  856. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  857. err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
  858. sizeof(struct udphdr), &ipc, &rt,
  859. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  860. if (err)
  861. udp_flush_pending_frames(sk);
  862. else if (!corkreq)
  863. err = udp_push_pending_frames(sk);
  864. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  865. up->pending = 0;
  866. release_sock(sk);
  867. out:
  868. ip_rt_put(rt);
  869. if (free)
  870. kfree(ipc.opt);
  871. if (!err)
  872. return len;
  873. /*
  874. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  875. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  876. * we don't have a good statistic (IpOutDiscards but it can be too many
  877. * things). We could add another new stat but at least for now that
  878. * seems like overkill.
  879. */
  880. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  881. UDP_INC_STATS_USER(sock_net(sk),
  882. UDP_MIB_SNDBUFERRORS, is_udplite);
  883. }
  884. return err;
  885. do_confirm:
  886. dst_confirm(&rt->u.dst);
  887. if (!(msg->msg_flags&MSG_PROBE) || len)
  888. goto back_from_confirm;
  889. err = 0;
  890. goto out;
  891. }
  892. EXPORT_SYMBOL(udp_sendmsg);
  893. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  894. size_t size, int flags)
  895. {
  896. struct udp_sock *up = udp_sk(sk);
  897. int ret;
  898. if (!up->pending) {
  899. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  900. /* Call udp_sendmsg to specify destination address which
  901. * sendpage interface can't pass.
  902. * This will succeed only when the socket is connected.
  903. */
  904. ret = udp_sendmsg(NULL, sk, &msg, 0);
  905. if (ret < 0)
  906. return ret;
  907. }
  908. lock_sock(sk);
  909. if (unlikely(!up->pending)) {
  910. release_sock(sk);
  911. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
  912. return -EINVAL;
  913. }
  914. ret = ip_append_page(sk, page, offset, size, flags);
  915. if (ret == -EOPNOTSUPP) {
  916. release_sock(sk);
  917. return sock_no_sendpage(sk->sk_socket, page, offset,
  918. size, flags);
  919. }
  920. if (ret < 0) {
  921. udp_flush_pending_frames(sk);
  922. goto out;
  923. }
  924. up->len += size;
  925. if (!(up->corkflag || (flags&MSG_MORE)))
  926. ret = udp_push_pending_frames(sk);
  927. if (!ret)
  928. ret = size;
  929. out:
  930. release_sock(sk);
  931. return ret;
  932. }
  933. /**
  934. * first_packet_length - return length of first packet in receive queue
  935. * @sk: socket
  936. *
  937. * Drops all bad checksum frames, until a valid one is found.
  938. * Returns the length of found skb, or 0 if none is found.
  939. */
  940. static unsigned int first_packet_length(struct sock *sk)
  941. {
  942. struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
  943. struct sk_buff *skb;
  944. unsigned int res;
  945. __skb_queue_head_init(&list_kill);
  946. spin_lock_bh(&rcvq->lock);
  947. while ((skb = skb_peek(rcvq)) != NULL &&
  948. udp_lib_checksum_complete(skb)) {
  949. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
  950. IS_UDPLITE(sk));
  951. atomic_inc(&sk->sk_drops);
  952. __skb_unlink(skb, rcvq);
  953. __skb_queue_tail(&list_kill, skb);
  954. }
  955. res = skb ? skb->len : 0;
  956. spin_unlock_bh(&rcvq->lock);
  957. if (!skb_queue_empty(&list_kill)) {
  958. bool slow = lock_sock_fast(sk);
  959. __skb_queue_purge(&list_kill);
  960. sk_mem_reclaim_partial(sk);
  961. unlock_sock_fast(sk, slow);
  962. }
  963. return res;
  964. }
  965. /*
  966. * IOCTL requests applicable to the UDP protocol
  967. */
  968. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  969. {
  970. switch (cmd) {
  971. case SIOCOUTQ:
  972. {
  973. int amount = sk_wmem_alloc_get(sk);
  974. return put_user(amount, (int __user *)arg);
  975. }
  976. case SIOCINQ:
  977. {
  978. unsigned int amount = first_packet_length(sk);
  979. if (amount)
  980. /*
  981. * We will only return the amount
  982. * of this packet since that is all
  983. * that will be read.
  984. */
  985. amount -= sizeof(struct udphdr);
  986. return put_user(amount, (int __user *)arg);
  987. }
  988. default:
  989. return -ENOIOCTLCMD;
  990. }
  991. return 0;
  992. }
  993. EXPORT_SYMBOL(udp_ioctl);
  994. /*
  995. * This should be easy, if there is something there we
  996. * return it, otherwise we block.
  997. */
  998. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  999. size_t len, int noblock, int flags, int *addr_len)
  1000. {
  1001. struct inet_sock *inet = inet_sk(sk);
  1002. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  1003. struct sk_buff *skb;
  1004. unsigned int ulen;
  1005. int peeked;
  1006. int err;
  1007. int is_udplite = IS_UDPLITE(sk);
  1008. bool slow;
  1009. /*
  1010. * Check any passed addresses
  1011. */
  1012. if (addr_len)
  1013. *addr_len = sizeof(*sin);
  1014. if (flags & MSG_ERRQUEUE)
  1015. return ip_recv_error(sk, msg, len);
  1016. try_again:
  1017. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  1018. &peeked, &err);
  1019. if (!skb)
  1020. goto out;
  1021. ulen = skb->len - sizeof(struct udphdr);
  1022. if (len > ulen)
  1023. len = ulen;
  1024. else if (len < ulen)
  1025. msg->msg_flags |= MSG_TRUNC;
  1026. /*
  1027. * If checksum is needed at all, try to do it while copying the
  1028. * data. If the data is truncated, or if we only want a partial
  1029. * coverage checksum (UDP-Lite), do it before the copy.
  1030. */
  1031. if (len < ulen || UDP_SKB_CB(skb)->partial_cov) {
  1032. if (udp_lib_checksum_complete(skb))
  1033. goto csum_copy_err;
  1034. }
  1035. if (skb_csum_unnecessary(skb))
  1036. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  1037. msg->msg_iov, len);
  1038. else {
  1039. err = skb_copy_and_csum_datagram_iovec(skb,
  1040. sizeof(struct udphdr),
  1041. msg->msg_iov);
  1042. if (err == -EINVAL)
  1043. goto csum_copy_err;
  1044. }
  1045. if (err)
  1046. goto out_free;
  1047. if (!peeked)
  1048. UDP_INC_STATS_USER(sock_net(sk),
  1049. UDP_MIB_INDATAGRAMS, is_udplite);
  1050. sock_recv_ts_and_drops(msg, sk, skb);
  1051. /* Copy the address. */
  1052. if (sin) {
  1053. sin->sin_family = AF_INET;
  1054. sin->sin_port = udp_hdr(skb)->source;
  1055. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  1056. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  1057. }
  1058. if (inet->cmsg_flags)
  1059. ip_cmsg_recv(msg, skb);
  1060. err = len;
  1061. if (flags & MSG_TRUNC)
  1062. err = ulen;
  1063. out_free:
  1064. skb_free_datagram_locked(sk, skb);
  1065. out:
  1066. return err;
  1067. csum_copy_err:
  1068. slow = lock_sock_fast(sk);
  1069. if (!skb_kill_datagram(sk, skb, flags))
  1070. UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1071. unlock_sock_fast(sk, slow);
  1072. if (noblock)
  1073. return -EAGAIN;
  1074. goto try_again;
  1075. }
  1076. int udp_disconnect(struct sock *sk, int flags)
  1077. {
  1078. struct inet_sock *inet = inet_sk(sk);
  1079. /*
  1080. * 1003.1g - break association.
  1081. */
  1082. sk->sk_state = TCP_CLOSE;
  1083. inet->inet_daddr = 0;
  1084. inet->inet_dport = 0;
  1085. sock_rps_save_rxhash(sk, 0);
  1086. sk->sk_bound_dev_if = 0;
  1087. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  1088. inet_reset_saddr(sk);
  1089. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  1090. sk->sk_prot->unhash(sk);
  1091. inet->inet_sport = 0;
  1092. }
  1093. sk_dst_reset(sk);
  1094. return 0;
  1095. }
  1096. EXPORT_SYMBOL(udp_disconnect);
  1097. void udp_lib_unhash(struct sock *sk)
  1098. {
  1099. if (sk_hashed(sk)) {
  1100. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  1101. struct udp_hslot *hslot, *hslot2;
  1102. hslot = udp_hashslot(udptable, sock_net(sk),
  1103. udp_sk(sk)->udp_port_hash);
  1104. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  1105. spin_lock_bh(&hslot->lock);
  1106. if (sk_nulls_del_node_init_rcu(sk)) {
  1107. hslot->count--;
  1108. inet_sk(sk)->inet_num = 0;
  1109. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  1110. spin_lock(&hslot2->lock);
  1111. hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
  1112. hslot2->count--;
  1113. spin_unlock(&hslot2->lock);
  1114. }
  1115. spin_unlock_bh(&hslot->lock);
  1116. }
  1117. }
  1118. EXPORT_SYMBOL(udp_lib_unhash);
  1119. static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1120. {
  1121. int rc;
  1122. if (inet_sk(sk)->inet_daddr)
  1123. sock_rps_save_rxhash(sk, skb->rxhash);
  1124. rc = ip_queue_rcv_skb(sk, skb);
  1125. if (rc < 0) {
  1126. int is_udplite = IS_UDPLITE(sk);
  1127. /* Note that an ENOMEM error is charged twice */
  1128. if (rc == -ENOMEM)
  1129. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  1130. is_udplite);
  1131. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1132. kfree_skb(skb);
  1133. return -1;
  1134. }
  1135. return 0;
  1136. }
  1137. /* returns:
  1138. * -1: error
  1139. * 0: success
  1140. * >0: "udp encap" protocol resubmission
  1141. *
  1142. * Note that in the success and error cases, the skb is assumed to
  1143. * have either been requeued or freed.
  1144. */
  1145. int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1146. {
  1147. struct udp_sock *up = udp_sk(sk);
  1148. int rc;
  1149. int is_udplite = IS_UDPLITE(sk);
  1150. /*
  1151. * Charge it to the socket, dropping if the queue is full.
  1152. */
  1153. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  1154. goto drop;
  1155. nf_reset(skb);
  1156. if (up->encap_type) {
  1157. /*
  1158. * This is an encapsulation socket so pass the skb to
  1159. * the socket's udp_encap_rcv() hook. Otherwise, just
  1160. * fall through and pass this up the UDP socket.
  1161. * up->encap_rcv() returns the following value:
  1162. * =0 if skb was successfully passed to the encap
  1163. * handler or was discarded by it.
  1164. * >0 if skb should be passed on to UDP.
  1165. * <0 if skb should be resubmitted as proto -N
  1166. */
  1167. /* if we're overly short, let UDP handle it */
  1168. if (skb->len > sizeof(struct udphdr) &&
  1169. up->encap_rcv != NULL) {
  1170. int ret;
  1171. ret = (*up->encap_rcv)(sk, skb);
  1172. if (ret <= 0) {
  1173. UDP_INC_STATS_BH(sock_net(sk),
  1174. UDP_MIB_INDATAGRAMS,
  1175. is_udplite);
  1176. return -ret;
  1177. }
  1178. }
  1179. /* FALLTHROUGH -- it's a UDP Packet */
  1180. }
  1181. /*
  1182. * UDP-Lite specific tests, ignored on UDP sockets
  1183. */
  1184. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  1185. /*
  1186. * MIB statistics other than incrementing the error count are
  1187. * disabled for the following two types of errors: these depend
  1188. * on the application settings, not on the functioning of the
  1189. * protocol stack as such.
  1190. *
  1191. * RFC 3828 here recommends (sec 3.3): "There should also be a
  1192. * way ... to ... at least let the receiving application block
  1193. * delivery of packets with coverage values less than a value
  1194. * provided by the application."
  1195. */
  1196. if (up->pcrlen == 0) { /* full coverage was set */
  1197. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  1198. "%d while full coverage %d requested\n",
  1199. UDP_SKB_CB(skb)->cscov, skb->len);
  1200. goto drop;
  1201. }
  1202. /* The next case involves violating the min. coverage requested
  1203. * by the receiver. This is subtle: if receiver wants x and x is
  1204. * greater than the buffersize/MTU then receiver will complain
  1205. * that it wants x while sender emits packets of smaller size y.
  1206. * Therefore the above ...()->partial_cov statement is essential.
  1207. */
  1208. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  1209. LIMIT_NETDEBUG(KERN_WARNING
  1210. "UDPLITE: coverage %d too small, need min %d\n",
  1211. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  1212. goto drop;
  1213. }
  1214. }
  1215. if (sk->sk_filter) {
  1216. if (udp_lib_checksum_complete(skb))
  1217. goto drop;
  1218. }
  1219. if (sk_rcvqueues_full(sk, skb))
  1220. goto drop;
  1221. rc = 0;
  1222. bh_lock_sock(sk);
  1223. if (!sock_owned_by_user(sk))
  1224. rc = __udp_queue_rcv_skb(sk, skb);
  1225. else if (sk_add_backlog(sk, skb)) {
  1226. bh_unlock_sock(sk);
  1227. goto drop;
  1228. }
  1229. bh_unlock_sock(sk);
  1230. return rc;
  1231. drop:
  1232. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1233. atomic_inc(&sk->sk_drops);
  1234. kfree_skb(skb);
  1235. return -1;
  1236. }
  1237. static void flush_stack(struct sock **stack, unsigned int count,
  1238. struct sk_buff *skb, unsigned int final)
  1239. {
  1240. unsigned int i;
  1241. struct sk_buff *skb1 = NULL;
  1242. struct sock *sk;
  1243. for (i = 0; i < count; i++) {
  1244. sk = stack[i];
  1245. if (likely(skb1 == NULL))
  1246. skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
  1247. if (!skb1) {
  1248. atomic_inc(&sk->sk_drops);
  1249. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  1250. IS_UDPLITE(sk));
  1251. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
  1252. IS_UDPLITE(sk));
  1253. }
  1254. if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0)
  1255. skb1 = NULL;
  1256. }
  1257. if (unlikely(skb1))
  1258. kfree_skb(skb1);
  1259. }
  1260. /*
  1261. * Multicasts and broadcasts go to each listener.
  1262. *
  1263. * Note: called only from the BH handler context.
  1264. */
  1265. static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  1266. struct udphdr *uh,
  1267. __be32 saddr, __be32 daddr,
  1268. struct udp_table *udptable)
  1269. {
  1270. struct sock *sk, *stack[256 / sizeof(struct sock *)];
  1271. struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
  1272. int dif;
  1273. unsigned int i, count = 0;
  1274. spin_lock(&hslot->lock);
  1275. sk = sk_nulls_head(&hslot->head);
  1276. dif = skb->dev->ifindex;
  1277. sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
  1278. while (sk) {
  1279. stack[count++] = sk;
  1280. sk = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
  1281. daddr, uh->source, saddr, dif);
  1282. if (unlikely(count == ARRAY_SIZE(stack))) {
  1283. if (!sk)
  1284. break;
  1285. flush_stack(stack, count, skb, ~0);
  1286. count = 0;
  1287. }
  1288. }
  1289. /*
  1290. * before releasing chain lock, we must take a reference on sockets
  1291. */
  1292. for (i = 0; i < count; i++)
  1293. sock_hold(stack[i]);
  1294. spin_unlock(&hslot->lock);
  1295. /*
  1296. * do the slow work with no lock held
  1297. */
  1298. if (count) {
  1299. flush_stack(stack, count, skb, count - 1);
  1300. for (i = 0; i < count; i++)
  1301. sock_put(stack[i]);
  1302. } else {
  1303. kfree_skb(skb);
  1304. }
  1305. return 0;
  1306. }
  1307. /* Initialize UDP checksum. If exited with zero value (success),
  1308. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  1309. * Otherwise, csum completion requires chacksumming packet body,
  1310. * including udp header and folding it to skb->csum.
  1311. */
  1312. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  1313. int proto)
  1314. {
  1315. const struct iphdr *iph;
  1316. int err;
  1317. UDP_SKB_CB(skb)->partial_cov = 0;
  1318. UDP_SKB_CB(skb)->cscov = skb->len;
  1319. if (proto == IPPROTO_UDPLITE) {
  1320. err = udplite_checksum_init(skb, uh);
  1321. if (err)
  1322. return err;
  1323. }
  1324. iph = ip_hdr(skb);
  1325. if (uh->check == 0) {
  1326. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1327. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1328. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1329. proto, skb->csum))
  1330. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1331. }
  1332. if (!skb_csum_unnecessary(skb))
  1333. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  1334. skb->len, proto, 0);
  1335. /* Probably, we should checksum udp header (it should be in cache
  1336. * in any case) and data in tiny packets (< rx copybreak).
  1337. */
  1338. return 0;
  1339. }
  1340. /*
  1341. * All we need to do is get the socket, and then do a checksum.
  1342. */
  1343. int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  1344. int proto)
  1345. {
  1346. struct sock *sk;
  1347. struct udphdr *uh;
  1348. unsigned short ulen;
  1349. struct rtable *rt = skb_rtable(skb);
  1350. __be32 saddr, daddr;
  1351. struct net *net = dev_net(skb->dev);
  1352. /*
  1353. * Validate the packet.
  1354. */
  1355. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1356. goto drop; /* No space for header. */
  1357. uh = udp_hdr(skb);
  1358. ulen = ntohs(uh->len);
  1359. saddr = ip_hdr(skb)->saddr;
  1360. daddr = ip_hdr(skb)->daddr;
  1361. if (ulen > skb->len)
  1362. goto short_packet;
  1363. if (proto == IPPROTO_UDP) {
  1364. /* UDP validates ulen. */
  1365. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1366. goto short_packet;
  1367. uh = udp_hdr(skb);
  1368. }
  1369. if (udp4_csum_init(skb, uh, proto))
  1370. goto csum_error;
  1371. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1372. return __udp4_lib_mcast_deliver(net, skb, uh,
  1373. saddr, daddr, udptable);
  1374. sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  1375. if (sk != NULL) {
  1376. int ret = udp_queue_rcv_skb(sk, skb);
  1377. sock_put(sk);
  1378. /* a return value > 0 means to resubmit the input, but
  1379. * it wants the return to be -protocol, or 0
  1380. */
  1381. if (ret > 0)
  1382. return -ret;
  1383. return 0;
  1384. }
  1385. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1386. goto drop;
  1387. nf_reset(skb);
  1388. /* No socket. Drop packet silently, if checksum is wrong */
  1389. if (udp_lib_checksum_complete(skb))
  1390. goto csum_error;
  1391. UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1392. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1393. /*
  1394. * Hmm. We got an UDP packet to a port to which we
  1395. * don't wanna listen. Ignore it.
  1396. */
  1397. kfree_skb(skb);
  1398. return 0;
  1399. short_packet:
  1400. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
  1401. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1402. &saddr,
  1403. ntohs(uh->source),
  1404. ulen,
  1405. skb->len,
  1406. &daddr,
  1407. ntohs(uh->dest));
  1408. goto drop;
  1409. csum_error:
  1410. /*
  1411. * RFC1122: OK. Discards the bad packet silently (as far as
  1412. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1413. */
  1414. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
  1415. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1416. &saddr,
  1417. ntohs(uh->source),
  1418. &daddr,
  1419. ntohs(uh->dest),
  1420. ulen);
  1421. drop:
  1422. UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1423. kfree_skb(skb);
  1424. return 0;
  1425. }
  1426. int udp_rcv(struct sk_buff *skb)
  1427. {
  1428. return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  1429. }
  1430. void udp_destroy_sock(struct sock *sk)
  1431. {
  1432. bool slow = lock_sock_fast(sk);
  1433. udp_flush_pending_frames(sk);
  1434. unlock_sock_fast(sk, slow);
  1435. }
  1436. /*
  1437. * Socket option code for UDP
  1438. */
  1439. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1440. char __user *optval, unsigned int optlen,
  1441. int (*push_pending_frames)(struct sock *))
  1442. {
  1443. struct udp_sock *up = udp_sk(sk);
  1444. int val;
  1445. int err = 0;
  1446. int is_udplite = IS_UDPLITE(sk);
  1447. if (optlen < sizeof(int))
  1448. return -EINVAL;
  1449. if (get_user(val, (int __user *)optval))
  1450. return -EFAULT;
  1451. switch (optname) {
  1452. case UDP_CORK:
  1453. if (val != 0) {
  1454. up->corkflag = 1;
  1455. } else {
  1456. up->corkflag = 0;
  1457. lock_sock(sk);
  1458. (*push_pending_frames)(sk);
  1459. release_sock(sk);
  1460. }
  1461. break;
  1462. case UDP_ENCAP:
  1463. switch (val) {
  1464. case 0:
  1465. case UDP_ENCAP_ESPINUDP:
  1466. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1467. up->encap_rcv = xfrm4_udp_encap_rcv;
  1468. /* FALLTHROUGH */
  1469. case UDP_ENCAP_L2TPINUDP:
  1470. up->encap_type = val;
  1471. break;
  1472. default:
  1473. err = -ENOPROTOOPT;
  1474. break;
  1475. }
  1476. break;
  1477. /*
  1478. * UDP-Lite's partial checksum coverage (RFC 3828).
  1479. */
  1480. /* The sender sets actual checksum coverage length via this option.
  1481. * The case coverage > packet length is handled by send module. */
  1482. case UDPLITE_SEND_CSCOV:
  1483. if (!is_udplite) /* Disable the option on UDP sockets */
  1484. return -ENOPROTOOPT;
  1485. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1486. val = 8;
  1487. else if (val > USHRT_MAX)
  1488. val = USHRT_MAX;
  1489. up->pcslen = val;
  1490. up->pcflag |= UDPLITE_SEND_CC;
  1491. break;
  1492. /* The receiver specifies a minimum checksum coverage value. To make
  1493. * sense, this should be set to at least 8 (as done below). If zero is
  1494. * used, this again means full checksum coverage. */
  1495. case UDPLITE_RECV_CSCOV:
  1496. if (!is_udplite) /* Disable the option on UDP sockets */
  1497. return -ENOPROTOOPT;
  1498. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1499. val = 8;
  1500. else if (val > USHRT_MAX)
  1501. val = USHRT_MAX;
  1502. up->pcrlen = val;
  1503. up->pcflag |= UDPLITE_RECV_CC;
  1504. break;
  1505. default:
  1506. err = -ENOPROTOOPT;
  1507. break;
  1508. }
  1509. return err;
  1510. }
  1511. EXPORT_SYMBOL(udp_lib_setsockopt);
  1512. int udp_setsockopt(struct sock *sk, int level, int optname,
  1513. char __user *optval, unsigned int optlen)
  1514. {
  1515. if (level == SOL_UDP || level == SOL_UDPLITE)
  1516. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1517. udp_push_pending_frames);
  1518. return ip_setsockopt(sk, level, optname, optval, optlen);
  1519. }
  1520. #ifdef CONFIG_COMPAT
  1521. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1522. char __user *optval, unsigned int optlen)
  1523. {
  1524. if (level == SOL_UDP || level == SOL_UDPLITE)
  1525. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1526. udp_push_pending_frames);
  1527. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1528. }
  1529. #endif
  1530. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1531. char __user *optval, int __user *optlen)
  1532. {
  1533. struct udp_sock *up = udp_sk(sk);
  1534. int val, len;
  1535. if (get_user(len, optlen))
  1536. return -EFAULT;
  1537. len = min_t(unsigned int, len, sizeof(int));
  1538. if (len < 0)
  1539. return -EINVAL;
  1540. switch (optname) {
  1541. case UDP_CORK:
  1542. val = up->corkflag;
  1543. break;
  1544. case UDP_ENCAP:
  1545. val = up->encap_type;
  1546. break;
  1547. /* The following two cannot be changed on UDP sockets, the return is
  1548. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1549. case UDPLITE_SEND_CSCOV:
  1550. val = up->pcslen;
  1551. break;
  1552. case UDPLITE_RECV_CSCOV:
  1553. val = up->pcrlen;
  1554. break;
  1555. default:
  1556. return -ENOPROTOOPT;
  1557. }
  1558. if (put_user(len, optlen))
  1559. return -EFAULT;
  1560. if (copy_to_user(optval, &val, len))
  1561. return -EFAULT;
  1562. return 0;
  1563. }
  1564. EXPORT_SYMBOL(udp_lib_getsockopt);
  1565. int udp_getsockopt(struct sock *sk, int level, int optname,
  1566. char __user *optval, int __user *optlen)
  1567. {
  1568. if (level == SOL_UDP || level == SOL_UDPLITE)
  1569. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1570. return ip_getsockopt(sk, level, optname, optval, optlen);
  1571. }
  1572. #ifdef CONFIG_COMPAT
  1573. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1574. char __user *optval, int __user *optlen)
  1575. {
  1576. if (level == SOL_UDP || level == SOL_UDPLITE)
  1577. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1578. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1579. }
  1580. #endif
  1581. /**
  1582. * udp_poll - wait for a UDP event.
  1583. * @file - file struct
  1584. * @sock - socket
  1585. * @wait - poll table
  1586. *
  1587. * This is same as datagram poll, except for the special case of
  1588. * blocking sockets. If application is using a blocking fd
  1589. * and a packet with checksum error is in the queue;
  1590. * then it could get return from select indicating data available
  1591. * but then block when reading it. Add special case code
  1592. * to work around these arguably broken applications.
  1593. */
  1594. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1595. {
  1596. unsigned int mask = datagram_poll(file, sock, wait);
  1597. struct sock *sk = sock->sk;
  1598. /* Check for false positives due to checksum errors */
  1599. if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
  1600. !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
  1601. mask &= ~(POLLIN | POLLRDNORM);
  1602. return mask;
  1603. }
  1604. EXPORT_SYMBOL(udp_poll);
  1605. struct proto udp_prot = {
  1606. .name = "UDP",
  1607. .owner = THIS_MODULE,
  1608. .close = udp_lib_close,
  1609. .connect = ip4_datagram_connect,
  1610. .disconnect = udp_disconnect,
  1611. .ioctl = udp_ioctl,
  1612. .destroy = udp_destroy_sock,
  1613. .setsockopt = udp_setsockopt,
  1614. .getsockopt = udp_getsockopt,
  1615. .sendmsg = udp_sendmsg,
  1616. .recvmsg = udp_recvmsg,
  1617. .sendpage = udp_sendpage,
  1618. .backlog_rcv = __udp_queue_rcv_skb,
  1619. .hash = udp_lib_hash,
  1620. .unhash = udp_lib_unhash,
  1621. .get_port = udp_v4_get_port,
  1622. .memory_allocated = &udp_memory_allocated,
  1623. .sysctl_mem = sysctl_udp_mem,
  1624. .sysctl_wmem = &sysctl_udp_wmem_min,
  1625. .sysctl_rmem = &sysctl_udp_rmem_min,
  1626. .obj_size = sizeof(struct udp_sock),
  1627. .slab_flags = SLAB_DESTROY_BY_RCU,
  1628. .h.udp_table = &udp_table,
  1629. #ifdef CONFIG_COMPAT
  1630. .compat_setsockopt = compat_udp_setsockopt,
  1631. .compat_getsockopt = compat_udp_getsockopt,
  1632. #endif
  1633. };
  1634. EXPORT_SYMBOL(udp_prot);
  1635. /* ------------------------------------------------------------------------ */
  1636. #ifdef CONFIG_PROC_FS
  1637. static struct sock *udp_get_first(struct seq_file *seq, int start)
  1638. {
  1639. struct sock *sk;
  1640. struct udp_iter_state *state = seq->private;
  1641. struct net *net = seq_file_net(seq);
  1642. for (state->bucket = start; state->bucket <= state->udp_table->mask;
  1643. ++state->bucket) {
  1644. struct hlist_nulls_node *node;
  1645. struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
  1646. if (hlist_nulls_empty(&hslot->head))
  1647. continue;
  1648. spin_lock_bh(&hslot->lock);
  1649. sk_nulls_for_each(sk, node, &hslot->head) {
  1650. if (!net_eq(sock_net(sk), net))
  1651. continue;
  1652. if (sk->sk_family == state->family)
  1653. goto found;
  1654. }
  1655. spin_unlock_bh(&hslot->lock);
  1656. }
  1657. sk = NULL;
  1658. found:
  1659. return sk;
  1660. }
  1661. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1662. {
  1663. struct udp_iter_state *state = seq->private;
  1664. struct net *net = seq_file_net(seq);
  1665. do {
  1666. sk = sk_nulls_next(sk);
  1667. } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
  1668. if (!sk) {
  1669. if (state->bucket <= state->udp_table->mask)
  1670. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  1671. return udp_get_first(seq, state->bucket + 1);
  1672. }
  1673. return sk;
  1674. }
  1675. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1676. {
  1677. struct sock *sk = udp_get_first(seq, 0);
  1678. if (sk)
  1679. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  1680. --pos;
  1681. return pos ? NULL : sk;
  1682. }
  1683. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1684. {
  1685. struct udp_iter_state *state = seq->private;
  1686. state->bucket = MAX_UDP_PORTS;
  1687. return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  1688. }
  1689. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1690. {
  1691. struct sock *sk;
  1692. if (v == SEQ_START_TOKEN)
  1693. sk = udp_get_idx(seq, 0);
  1694. else
  1695. sk = udp_get_next(seq, v);
  1696. ++*pos;
  1697. return sk;
  1698. }
  1699. static void udp_seq_stop(struct seq_file *seq, void *v)
  1700. {
  1701. struct udp_iter_state *state = seq->private;
  1702. if (state->bucket <= state->udp_table->mask)
  1703. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  1704. }
  1705. static int udp_seq_open(struct inode *inode, struct file *file)
  1706. {
  1707. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1708. struct udp_iter_state *s;
  1709. int err;
  1710. err = seq_open_net(inode, file, &afinfo->seq_ops,
  1711. sizeof(struct udp_iter_state));
  1712. if (err < 0)
  1713. return err;
  1714. s = ((struct seq_file *)file->private_data)->private;
  1715. s->family = afinfo->family;
  1716. s->udp_table = afinfo->udp_table;
  1717. return err;
  1718. }
  1719. /* ------------------------------------------------------------------------ */
  1720. int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
  1721. {
  1722. struct proc_dir_entry *p;
  1723. int rc = 0;
  1724. afinfo->seq_fops.open = udp_seq_open;
  1725. afinfo->seq_fops.read = seq_read;
  1726. afinfo->seq_fops.llseek = seq_lseek;
  1727. afinfo->seq_fops.release = seq_release_net;
  1728. afinfo->seq_ops.start = udp_seq_start;
  1729. afinfo->seq_ops.next = udp_seq_next;
  1730. afinfo->seq_ops.stop = udp_seq_stop;
  1731. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  1732. &afinfo->seq_fops, afinfo);
  1733. if (!p)
  1734. rc = -ENOMEM;
  1735. return rc;
  1736. }
  1737. EXPORT_SYMBOL(udp_proc_register);
  1738. void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
  1739. {
  1740. proc_net_remove(net, afinfo->name);
  1741. }
  1742. EXPORT_SYMBOL(udp_proc_unregister);
  1743. /* ------------------------------------------------------------------------ */
  1744. static void udp4_format_sock(struct sock *sp, struct seq_file *f,
  1745. int bucket, int *len)
  1746. {
  1747. struct inet_sock *inet = inet_sk(sp);
  1748. __be32 dest = inet->inet_daddr;
  1749. __be32 src = inet->inet_rcv_saddr;
  1750. __u16 destp = ntohs(inet->inet_dport);
  1751. __u16 srcp = ntohs(inet->inet_sport);
  1752. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  1753. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
  1754. bucket, src, srcp, dest, destp, sp->sk_state,
  1755. sk_wmem_alloc_get(sp),
  1756. sk_rmem_alloc_get(sp),
  1757. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1758. atomic_read(&sp->sk_refcnt), sp,
  1759. atomic_read(&sp->sk_drops), len);
  1760. }
  1761. int udp4_seq_show(struct seq_file *seq, void *v)
  1762. {
  1763. if (v == SEQ_START_TOKEN)
  1764. seq_printf(seq, "%-127s\n",
  1765. " sl local_address rem_address st tx_queue "
  1766. "rx_queue tr tm->when retrnsmt uid timeout "
  1767. "inode ref pointer drops");
  1768. else {
  1769. struct udp_iter_state *state = seq->private;
  1770. int len;
  1771. udp4_format_sock(v, seq, state->bucket, &len);
  1772. seq_printf(seq, "%*s\n", 127 - len, "");
  1773. }
  1774. return 0;
  1775. }
  1776. /* ------------------------------------------------------------------------ */
  1777. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1778. .name = "udp",
  1779. .family = AF_INET,
  1780. .udp_table = &udp_table,
  1781. .seq_fops = {
  1782. .owner = THIS_MODULE,
  1783. },
  1784. .seq_ops = {
  1785. .show = udp4_seq_show,
  1786. },
  1787. };
  1788. static int __net_init udp4_proc_init_net(struct net *net)
  1789. {
  1790. return udp_proc_register(net, &udp4_seq_afinfo);
  1791. }
  1792. static void __net_exit udp4_proc_exit_net(struct net *net)
  1793. {
  1794. udp_proc_unregister(net, &udp4_seq_afinfo);
  1795. }
  1796. static struct pernet_operations udp4_net_ops = {
  1797. .init = udp4_proc_init_net,
  1798. .exit = udp4_proc_exit_net,
  1799. };
  1800. int __init udp4_proc_init(void)
  1801. {
  1802. return register_pernet_subsys(&udp4_net_ops);
  1803. }
  1804. void udp4_proc_exit(void)
  1805. {
  1806. unregister_pernet_subsys(&udp4_net_ops);
  1807. }
  1808. #endif /* CONFIG_PROC_FS */
  1809. static __initdata unsigned long uhash_entries;
  1810. static int __init set_uhash_entries(char *str)
  1811. {
  1812. if (!str)
  1813. return 0;
  1814. uhash_entries = simple_strtoul(str, &str, 0);
  1815. if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
  1816. uhash_entries = UDP_HTABLE_SIZE_MIN;
  1817. return 1;
  1818. }
  1819. __setup("uhash_entries=", set_uhash_entries);
  1820. void __init udp_table_init(struct udp_table *table, const char *name)
  1821. {
  1822. unsigned int i;
  1823. if (!CONFIG_BASE_SMALL)
  1824. table->hash = alloc_large_system_hash(name,
  1825. 2 * sizeof(struct udp_hslot),
  1826. uhash_entries,
  1827. 21, /* one slot per 2 MB */
  1828. 0,
  1829. &table->log,
  1830. &table->mask,
  1831. 64 * 1024);
  1832. /*
  1833. * Make sure hash table has the minimum size
  1834. */
  1835. if (CONFIG_BASE_SMALL || table->mask < UDP_HTABLE_SIZE_MIN - 1) {
  1836. table->hash = kmalloc(UDP_HTABLE_SIZE_MIN *
  1837. 2 * sizeof(struct udp_hslot), GFP_KERNEL);
  1838. if (!table->hash)
  1839. panic(name);
  1840. table->log = ilog2(UDP_HTABLE_SIZE_MIN);
  1841. table->mask = UDP_HTABLE_SIZE_MIN - 1;
  1842. }
  1843. table->hash2 = table->hash + (table->mask + 1);
  1844. for (i = 0; i <= table->mask; i++) {
  1845. INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
  1846. table->hash[i].count = 0;
  1847. spin_lock_init(&table->hash[i].lock);
  1848. }
  1849. for (i = 0; i <= table->mask; i++) {
  1850. INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
  1851. table->hash2[i].count = 0;
  1852. spin_lock_init(&table->hash2[i].lock);
  1853. }
  1854. }
  1855. void __init udp_init(void)
  1856. {
  1857. unsigned long nr_pages, limit;
  1858. udp_table_init(&udp_table, "UDP");
  1859. /* Set the pressure threshold up by the same strategy of TCP. It is a
  1860. * fraction of global memory that is up to 1/2 at 256 MB, decreasing
  1861. * toward zero with the amount of memory, with a floor of 128 pages.
  1862. */
  1863. nr_pages = totalram_pages - totalhigh_pages;
  1864. limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
  1865. limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
  1866. limit = max(limit, 128UL);
  1867. sysctl_udp_mem[0] = limit / 4 * 3;
  1868. sysctl_udp_mem[1] = limit;
  1869. sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
  1870. sysctl_udp_rmem_min = SK_MEM_QUANTUM;
  1871. sysctl_udp_wmem_min = SK_MEM_QUANTUM;
  1872. }
  1873. int udp4_ufo_send_check(struct sk_buff *skb)
  1874. {
  1875. const struct iphdr *iph;
  1876. struct udphdr *uh;
  1877. if (!pskb_may_pull(skb, sizeof(*uh)))
  1878. return -EINVAL;
  1879. iph = ip_hdr(skb);
  1880. uh = udp_hdr(skb);
  1881. uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1882. IPPROTO_UDP, 0);
  1883. skb->csum_start = skb_transport_header(skb) - skb->head;
  1884. skb->csum_offset = offsetof(struct udphdr, check);
  1885. skb->ip_summed = CHECKSUM_PARTIAL;
  1886. return 0;
  1887. }
  1888. struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, int features)
  1889. {
  1890. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1891. unsigned int mss;
  1892. int offset;
  1893. __wsum csum;
  1894. mss = skb_shinfo(skb)->gso_size;
  1895. if (unlikely(skb->len <= mss))
  1896. goto out;
  1897. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  1898. /* Packet is from an untrusted source, reset gso_segs. */
  1899. int type = skb_shinfo(skb)->gso_type;
  1900. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
  1901. !(type & (SKB_GSO_UDP))))
  1902. goto out;
  1903. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  1904. segs = NULL;
  1905. goto out;
  1906. }
  1907. /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
  1908. * do checksum of UDP packets sent as multiple IP fragments.
  1909. */
  1910. offset = skb->csum_start - skb_headroom(skb);
  1911. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1912. offset += skb->csum_offset;
  1913. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1914. skb->ip_summed = CHECKSUM_NONE;
  1915. /* Fragment the skb. IP headers of the fragments are updated in
  1916. * inet_gso_segment()
  1917. */
  1918. segs = skb_segment(skb, features);
  1919. out:
  1920. return segs;
  1921. }