udp.c 65 KB

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