udp.c 64 KB

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