udp.c 57 KB

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