udp.c 58 KB

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