udp.c 65 KB

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