udp.c 64 KB

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