udp.c 65 KB

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