udp.c 64 KB

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