udp.c 64 KB

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