udp.c 65 KB

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