udp.c 65 KB

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