udp.c 67 KB

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