udp.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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. #include <asm/system.h>
  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 <net/tcp_states.h>
  98. #include <linux/skbuff.h>
  99. #include <linux/proc_fs.h>
  100. #include <linux/seq_file.h>
  101. #include <net/net_namespace.h>
  102. #include <net/icmp.h>
  103. #include <net/route.h>
  104. #include <net/checksum.h>
  105. #include <net/xfrm.h>
  106. #include "udp_impl.h"
  107. struct udp_table udp_table __read_mostly;
  108. EXPORT_SYMBOL(udp_table);
  109. int sysctl_udp_mem[3] __read_mostly;
  110. EXPORT_SYMBOL(sysctl_udp_mem);
  111. int sysctl_udp_rmem_min __read_mostly;
  112. EXPORT_SYMBOL(sysctl_udp_rmem_min);
  113. int sysctl_udp_wmem_min __read_mostly;
  114. EXPORT_SYMBOL(sysctl_udp_wmem_min);
  115. atomic_t udp_memory_allocated;
  116. EXPORT_SYMBOL(udp_memory_allocated);
  117. #define MAX_UDP_PORTS 65536
  118. #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
  119. static int udp_lib_lport_inuse(struct net *net, __u16 num,
  120. const struct udp_hslot *hslot,
  121. unsigned long *bitmap,
  122. struct sock *sk,
  123. int (*saddr_comp)(const struct sock *sk1,
  124. const struct sock *sk2),
  125. unsigned int log)
  126. {
  127. struct sock *sk2;
  128. struct hlist_nulls_node *node;
  129. sk_nulls_for_each(sk2, node, &hslot->head)
  130. if (net_eq(sock_net(sk2), net) &&
  131. sk2 != sk &&
  132. (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
  133. (!sk2->sk_reuse || !sk->sk_reuse) &&
  134. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
  135. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  136. (*saddr_comp)(sk, sk2)) {
  137. if (bitmap)
  138. __set_bit(udp_sk(sk2)->udp_port_hash >> log,
  139. bitmap);
  140. else
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. /*
  146. * Note: we still hold spinlock of primary hash chain, so no other writer
  147. * can insert/delete a socket with local_port == num
  148. */
  149. static int udp_lib_lport_inuse2(struct net *net, __u16 num,
  150. struct udp_hslot *hslot2,
  151. struct sock *sk,
  152. int (*saddr_comp)(const struct sock *sk1,
  153. const struct sock *sk2))
  154. {
  155. struct sock *sk2;
  156. struct hlist_nulls_node *node;
  157. int res = 0;
  158. spin_lock(&hslot2->lock);
  159. udp_portaddr_for_each_entry(sk2, node, &hslot2->head)
  160. if (net_eq(sock_net(sk2), net) &&
  161. sk2 != sk &&
  162. (udp_sk(sk2)->udp_port_hash == num) &&
  163. (!sk2->sk_reuse || !sk->sk_reuse) &&
  164. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
  165. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  166. (*saddr_comp)(sk, sk2)) {
  167. res = 1;
  168. break;
  169. }
  170. spin_unlock(&hslot2->lock);
  171. return res;
  172. }
  173. /**
  174. * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
  175. *
  176. * @sk: socket struct in question
  177. * @snum: port number to look up
  178. * @saddr_comp: AF-dependent comparison of bound local IP addresses
  179. * @hash2_nulladdr: AF-dependant hash value in secondary hash chains,
  180. * with NULL address
  181. */
  182. int udp_lib_get_port(struct sock *sk, unsigned short snum,
  183. int (*saddr_comp)(const struct sock *sk1,
  184. const struct sock *sk2),
  185. unsigned int hash2_nulladdr)
  186. {
  187. struct udp_hslot *hslot, *hslot2;
  188. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  189. int error = 1;
  190. struct net *net = sock_net(sk);
  191. if (!snum) {
  192. int low, high, remaining;
  193. unsigned rand;
  194. unsigned short first, last;
  195. DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
  196. inet_get_local_port_range(&low, &high);
  197. remaining = (high - low) + 1;
  198. rand = net_random();
  199. first = (((u64)rand * remaining) >> 32) + low;
  200. /*
  201. * force rand to be an odd multiple of UDP_HTABLE_SIZE
  202. */
  203. rand = (rand | 1) * (udptable->mask + 1);
  204. for (last = first + udptable->mask + 1;
  205. first != last;
  206. first++) {
  207. hslot = udp_hashslot(udptable, net, first);
  208. bitmap_zero(bitmap, PORTS_PER_CHAIN);
  209. spin_lock_bh(&hslot->lock);
  210. udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
  211. saddr_comp, udptable->log);
  212. snum = first;
  213. /*
  214. * Iterate on all possible values of snum for this hash.
  215. * Using steps of an odd multiple of UDP_HTABLE_SIZE
  216. * give us randomization and full range coverage.
  217. */
  218. do {
  219. if (low <= snum && snum <= high &&
  220. !test_bit(snum >> udptable->log, bitmap))
  221. goto found;
  222. snum += rand;
  223. } while (snum != first);
  224. spin_unlock_bh(&hslot->lock);
  225. }
  226. goto fail;
  227. } else {
  228. hslot = udp_hashslot(udptable, net, snum);
  229. spin_lock_bh(&hslot->lock);
  230. if (hslot->count > 10) {
  231. int exist;
  232. unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
  233. slot2 &= udptable->mask;
  234. hash2_nulladdr &= udptable->mask;
  235. hslot2 = udp_hashslot2(udptable, slot2);
  236. if (hslot->count < hslot2->count)
  237. goto scan_primary_hash;
  238. exist = udp_lib_lport_inuse2(net, snum, hslot2,
  239. sk, saddr_comp);
  240. if (!exist && (hash2_nulladdr != slot2)) {
  241. hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
  242. exist = udp_lib_lport_inuse2(net, snum, hslot2,
  243. sk, saddr_comp);
  244. }
  245. if (exist)
  246. goto fail_unlock;
  247. else
  248. goto found;
  249. }
  250. scan_primary_hash:
  251. if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
  252. saddr_comp, 0))
  253. goto fail_unlock;
  254. }
  255. found:
  256. inet_sk(sk)->inet_num = snum;
  257. udp_sk(sk)->udp_port_hash = snum;
  258. udp_sk(sk)->udp_portaddr_hash ^= snum;
  259. if (sk_unhashed(sk)) {
  260. sk_nulls_add_node_rcu(sk, &hslot->head);
  261. hslot->count++;
  262. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  263. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  264. spin_lock(&hslot2->lock);
  265. hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
  266. &hslot2->head);
  267. hslot2->count++;
  268. spin_unlock(&hslot2->lock);
  269. }
  270. error = 0;
  271. fail_unlock:
  272. spin_unlock_bh(&hslot->lock);
  273. fail:
  274. return error;
  275. }
  276. EXPORT_SYMBOL(udp_lib_get_port);
  277. static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
  278. {
  279. struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
  280. return (!ipv6_only_sock(sk2) &&
  281. (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr ||
  282. inet1->inet_rcv_saddr == inet2->inet_rcv_saddr));
  283. }
  284. static unsigned int udp4_portaddr_hash(struct net *net, __be32 saddr,
  285. unsigned int port)
  286. {
  287. return jhash_1word(saddr, net_hash_mix(net)) ^ port;
  288. }
  289. int udp_v4_get_port(struct sock *sk, unsigned short snum)
  290. {
  291. unsigned int hash2_nulladdr =
  292. udp4_portaddr_hash(sock_net(sk), INADDR_ANY, snum);
  293. unsigned int hash2_partial =
  294. udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
  295. /* precompute partial secondary hash */
  296. udp_sk(sk)->udp_portaddr_hash = hash2_partial;
  297. return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr);
  298. }
  299. static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
  300. unsigned short hnum,
  301. __be16 sport, __be32 daddr, __be16 dport, int dif)
  302. {
  303. int score = -1;
  304. if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
  305. !ipv6_only_sock(sk)) {
  306. struct inet_sock *inet = inet_sk(sk);
  307. score = (sk->sk_family == PF_INET ? 1 : 0);
  308. if (inet->inet_rcv_saddr) {
  309. if (inet->inet_rcv_saddr != daddr)
  310. return -1;
  311. score += 2;
  312. }
  313. if (inet->inet_daddr) {
  314. if (inet->inet_daddr != saddr)
  315. return -1;
  316. score += 2;
  317. }
  318. if (inet->inet_dport) {
  319. if (inet->inet_dport != sport)
  320. return -1;
  321. score += 2;
  322. }
  323. if (sk->sk_bound_dev_if) {
  324. if (sk->sk_bound_dev_if != dif)
  325. return -1;
  326. score += 2;
  327. }
  328. }
  329. return score;
  330. }
  331. /*
  332. * In this second variant, we check (daddr, dport) matches (inet_rcv_sadd, inet_num)
  333. */
  334. #define SCORE2_MAX (1 + 2 + 2 + 2)
  335. static inline int compute_score2(struct sock *sk, struct net *net,
  336. __be32 saddr, __be16 sport,
  337. __be32 daddr, unsigned int hnum, int dif)
  338. {
  339. int score = -1;
  340. if (net_eq(sock_net(sk), net) && !ipv6_only_sock(sk)) {
  341. struct inet_sock *inet = inet_sk(sk);
  342. if (inet->inet_rcv_saddr != daddr)
  343. return -1;
  344. if (inet->inet_num != hnum)
  345. return -1;
  346. score = (sk->sk_family == PF_INET ? 1 : 0);
  347. if (inet->inet_daddr) {
  348. if (inet->inet_daddr != saddr)
  349. return -1;
  350. score += 2;
  351. }
  352. if (inet->inet_dport) {
  353. if (inet->inet_dport != sport)
  354. return -1;
  355. score += 2;
  356. }
  357. if (sk->sk_bound_dev_if) {
  358. if (sk->sk_bound_dev_if != dif)
  359. return -1;
  360. score += 2;
  361. }
  362. }
  363. return score;
  364. }
  365. /* called with read_rcu_lock() */
  366. static struct sock *udp4_lib_lookup2(struct net *net,
  367. __be32 saddr, __be16 sport,
  368. __be32 daddr, unsigned int hnum, int dif,
  369. struct udp_hslot *hslot2, unsigned int slot2)
  370. {
  371. struct sock *sk, *result;
  372. struct hlist_nulls_node *node;
  373. int score, badness;
  374. begin:
  375. result = NULL;
  376. badness = -1;
  377. udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
  378. score = compute_score2(sk, net, saddr, sport,
  379. daddr, hnum, dif);
  380. if (score > badness) {
  381. result = sk;
  382. badness = score;
  383. if (score == SCORE2_MAX)
  384. goto exact_match;
  385. }
  386. }
  387. /*
  388. * if the nulls value we got at the end of this lookup is
  389. * not the expected one, we must restart lookup.
  390. * We probably met an item that was moved to another chain.
  391. */
  392. if (get_nulls_value(node) != slot2)
  393. goto begin;
  394. if (result) {
  395. exact_match:
  396. if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
  397. result = NULL;
  398. else if (unlikely(compute_score2(result, net, saddr, sport,
  399. daddr, hnum, dif) < badness)) {
  400. sock_put(result);
  401. goto begin;
  402. }
  403. }
  404. return result;
  405. }
  406. /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
  407. * harder than this. -DaveM
  408. */
  409. static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
  410. __be16 sport, __be32 daddr, __be16 dport,
  411. int dif, struct udp_table *udptable)
  412. {
  413. struct sock *sk, *result;
  414. struct hlist_nulls_node *node;
  415. unsigned short hnum = ntohs(dport);
  416. unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
  417. struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
  418. int score, badness;
  419. rcu_read_lock();
  420. if (hslot->count > 10) {
  421. hash2 = udp4_portaddr_hash(net, daddr, hnum);
  422. slot2 = hash2 & udptable->mask;
  423. hslot2 = &udptable->hash2[slot2];
  424. if (hslot->count < hslot2->count)
  425. goto begin;
  426. result = udp4_lib_lookup2(net, saddr, sport,
  427. daddr, hnum, dif,
  428. hslot2, slot2);
  429. if (!result) {
  430. hash2 = udp4_portaddr_hash(net, INADDR_ANY, hnum);
  431. slot2 = hash2 & udptable->mask;
  432. hslot2 = &udptable->hash2[slot2];
  433. if (hslot->count < hslot2->count)
  434. goto begin;
  435. result = udp4_lib_lookup2(net, INADDR_ANY, sport,
  436. daddr, hnum, dif,
  437. hslot2, slot2);
  438. }
  439. rcu_read_unlock();
  440. return result;
  441. }
  442. begin:
  443. result = NULL;
  444. badness = -1;
  445. sk_nulls_for_each_rcu(sk, node, &hslot->head) {
  446. score = compute_score(sk, net, saddr, hnum, sport,
  447. daddr, dport, dif);
  448. if (score > badness) {
  449. result = sk;
  450. badness = score;
  451. }
  452. }
  453. /*
  454. * if the nulls value we got at the end of this lookup is
  455. * not the expected one, we must restart lookup.
  456. * We probably met an item that was moved to another chain.
  457. */
  458. if (get_nulls_value(node) != slot)
  459. goto begin;
  460. if (result) {
  461. if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
  462. result = NULL;
  463. else if (unlikely(compute_score(result, net, saddr, hnum, sport,
  464. daddr, dport, dif) < badness)) {
  465. sock_put(result);
  466. goto begin;
  467. }
  468. }
  469. rcu_read_unlock();
  470. return result;
  471. }
  472. static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
  473. __be16 sport, __be16 dport,
  474. struct udp_table *udptable)
  475. {
  476. struct sock *sk;
  477. const struct iphdr *iph = ip_hdr(skb);
  478. if (unlikely(sk = skb_steal_sock(skb)))
  479. return sk;
  480. else
  481. return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
  482. iph->daddr, dport, inet_iif(skb),
  483. udptable);
  484. }
  485. struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
  486. __be32 daddr, __be16 dport, int dif)
  487. {
  488. return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
  489. }
  490. EXPORT_SYMBOL_GPL(udp4_lib_lookup);
  491. static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
  492. __be16 loc_port, __be32 loc_addr,
  493. __be16 rmt_port, __be32 rmt_addr,
  494. int dif)
  495. {
  496. struct hlist_nulls_node *node;
  497. struct sock *s = sk;
  498. unsigned short hnum = ntohs(loc_port);
  499. sk_nulls_for_each_from(s, node) {
  500. struct inet_sock *inet = inet_sk(s);
  501. if (!net_eq(sock_net(s), net) ||
  502. udp_sk(s)->udp_port_hash != hnum ||
  503. (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
  504. (inet->inet_dport != rmt_port && inet->inet_dport) ||
  505. (inet->inet_rcv_saddr &&
  506. inet->inet_rcv_saddr != loc_addr) ||
  507. ipv6_only_sock(s) ||
  508. (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
  509. continue;
  510. if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
  511. continue;
  512. goto found;
  513. }
  514. s = NULL;
  515. found:
  516. return s;
  517. }
  518. /*
  519. * This routine is called by the ICMP module when it gets some
  520. * sort of error condition. If err < 0 then the socket should
  521. * be closed and the error returned to the user. If err > 0
  522. * it's just the icmp type << 8 | icmp code.
  523. * Header points to the ip header of the error packet. We move
  524. * on past this. Then (as it used to claim before adjustment)
  525. * header points to the first 8 bytes of the udp header. We need
  526. * to find the appropriate port.
  527. */
  528. void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
  529. {
  530. struct inet_sock *inet;
  531. struct iphdr *iph = (struct iphdr *)skb->data;
  532. struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
  533. const int type = icmp_hdr(skb)->type;
  534. const int code = icmp_hdr(skb)->code;
  535. struct sock *sk;
  536. int harderr;
  537. int err;
  538. struct net *net = dev_net(skb->dev);
  539. sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
  540. iph->saddr, uh->source, skb->dev->ifindex, udptable);
  541. if (sk == NULL) {
  542. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  543. return; /* No socket for error */
  544. }
  545. err = 0;
  546. harderr = 0;
  547. inet = inet_sk(sk);
  548. switch (type) {
  549. default:
  550. case ICMP_TIME_EXCEEDED:
  551. err = EHOSTUNREACH;
  552. break;
  553. case ICMP_SOURCE_QUENCH:
  554. goto out;
  555. case ICMP_PARAMETERPROB:
  556. err = EPROTO;
  557. harderr = 1;
  558. break;
  559. case ICMP_DEST_UNREACH:
  560. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  561. if (inet->pmtudisc != IP_PMTUDISC_DONT) {
  562. err = EMSGSIZE;
  563. harderr = 1;
  564. break;
  565. }
  566. goto out;
  567. }
  568. err = EHOSTUNREACH;
  569. if (code <= NR_ICMP_UNREACH) {
  570. harderr = icmp_err_convert[code].fatal;
  571. err = icmp_err_convert[code].errno;
  572. }
  573. break;
  574. }
  575. /*
  576. * RFC1122: OK. Passes ICMP errors back to application, as per
  577. * 4.1.3.3.
  578. */
  579. if (!inet->recverr) {
  580. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  581. goto out;
  582. } else {
  583. ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
  584. }
  585. sk->sk_err = err;
  586. sk->sk_error_report(sk);
  587. out:
  588. sock_put(sk);
  589. }
  590. void udp_err(struct sk_buff *skb, u32 info)
  591. {
  592. __udp4_lib_err(skb, info, &udp_table);
  593. }
  594. /*
  595. * Throw away all pending data and cancel the corking. Socket is locked.
  596. */
  597. void udp_flush_pending_frames(struct sock *sk)
  598. {
  599. struct udp_sock *up = udp_sk(sk);
  600. if (up->pending) {
  601. up->len = 0;
  602. up->pending = 0;
  603. ip_flush_pending_frames(sk);
  604. }
  605. }
  606. EXPORT_SYMBOL(udp_flush_pending_frames);
  607. /**
  608. * udp4_hwcsum_outgoing - handle outgoing HW checksumming
  609. * @sk: socket we are sending on
  610. * @skb: sk_buff containing the filled-in UDP header
  611. * (checksum field must be zeroed out)
  612. */
  613. static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  614. __be32 src, __be32 dst, int len)
  615. {
  616. unsigned int offset;
  617. struct udphdr *uh = udp_hdr(skb);
  618. __wsum csum = 0;
  619. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  620. /*
  621. * Only one fragment on the socket.
  622. */
  623. skb->csum_start = skb_transport_header(skb) - skb->head;
  624. skb->csum_offset = offsetof(struct udphdr, check);
  625. uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
  626. } else {
  627. /*
  628. * HW-checksum won't work as there are two or more
  629. * fragments on the socket so that all csums of sk_buffs
  630. * should be together
  631. */
  632. offset = skb_transport_offset(skb);
  633. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  634. skb->ip_summed = CHECKSUM_NONE;
  635. skb_queue_walk(&sk->sk_write_queue, skb) {
  636. csum = csum_add(csum, skb->csum);
  637. }
  638. uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
  639. if (uh->check == 0)
  640. uh->check = CSUM_MANGLED_0;
  641. }
  642. }
  643. /*
  644. * Push out all pending data as one UDP datagram. Socket is locked.
  645. */
  646. static int udp_push_pending_frames(struct sock *sk)
  647. {
  648. struct udp_sock *up = udp_sk(sk);
  649. struct inet_sock *inet = inet_sk(sk);
  650. struct flowi *fl = &inet->cork.fl;
  651. struct sk_buff *skb;
  652. struct udphdr *uh;
  653. int err = 0;
  654. int is_udplite = IS_UDPLITE(sk);
  655. __wsum csum = 0;
  656. /* Grab the skbuff where UDP header space exists. */
  657. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  658. goto out;
  659. /*
  660. * Create a UDP header
  661. */
  662. uh = udp_hdr(skb);
  663. uh->source = fl->fl_ip_sport;
  664. uh->dest = fl->fl_ip_dport;
  665. uh->len = htons(up->len);
  666. uh->check = 0;
  667. if (is_udplite) /* UDP-Lite */
  668. csum = udplite_csum_outgoing(sk, skb);
  669. else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
  670. skb->ip_summed = CHECKSUM_NONE;
  671. goto send;
  672. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  673. udp4_hwcsum_outgoing(sk, skb, fl->fl4_src, fl->fl4_dst, up->len);
  674. goto send;
  675. } else /* `normal' UDP */
  676. csum = udp_csum_outgoing(sk, skb);
  677. /* add protocol-dependent pseudo-header */
  678. uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
  679. sk->sk_protocol, csum);
  680. if (uh->check == 0)
  681. uh->check = CSUM_MANGLED_0;
  682. send:
  683. err = ip_push_pending_frames(sk);
  684. if (err) {
  685. if (err == -ENOBUFS && !inet->recverr) {
  686. UDP_INC_STATS_USER(sock_net(sk),
  687. UDP_MIB_SNDBUFERRORS, is_udplite);
  688. err = 0;
  689. }
  690. } else
  691. UDP_INC_STATS_USER(sock_net(sk),
  692. UDP_MIB_OUTDATAGRAMS, is_udplite);
  693. out:
  694. up->len = 0;
  695. up->pending = 0;
  696. return err;
  697. }
  698. int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  699. size_t len)
  700. {
  701. struct inet_sock *inet = inet_sk(sk);
  702. struct udp_sock *up = udp_sk(sk);
  703. int ulen = len;
  704. struct ipcm_cookie ipc;
  705. struct rtable *rt = NULL;
  706. int free = 0;
  707. int connected = 0;
  708. __be32 daddr, faddr, saddr;
  709. __be16 dport;
  710. u8 tos;
  711. int err, is_udplite = IS_UDPLITE(sk);
  712. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  713. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  714. if (len > 0xFFFF)
  715. return -EMSGSIZE;
  716. /*
  717. * Check the flags.
  718. */
  719. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
  720. return -EOPNOTSUPP;
  721. ipc.opt = NULL;
  722. ipc.shtx.flags = 0;
  723. if (up->pending) {
  724. /*
  725. * There are pending frames.
  726. * The socket lock must be held while it's corked.
  727. */
  728. lock_sock(sk);
  729. if (likely(up->pending)) {
  730. if (unlikely(up->pending != AF_INET)) {
  731. release_sock(sk);
  732. return -EINVAL;
  733. }
  734. goto do_append_data;
  735. }
  736. release_sock(sk);
  737. }
  738. ulen += sizeof(struct udphdr);
  739. /*
  740. * Get and verify the address.
  741. */
  742. if (msg->msg_name) {
  743. struct sockaddr_in * usin = (struct sockaddr_in *)msg->msg_name;
  744. if (msg->msg_namelen < sizeof(*usin))
  745. return -EINVAL;
  746. if (usin->sin_family != AF_INET) {
  747. if (usin->sin_family != AF_UNSPEC)
  748. return -EAFNOSUPPORT;
  749. }
  750. daddr = usin->sin_addr.s_addr;
  751. dport = usin->sin_port;
  752. if (dport == 0)
  753. return -EINVAL;
  754. } else {
  755. if (sk->sk_state != TCP_ESTABLISHED)
  756. return -EDESTADDRREQ;
  757. daddr = inet->inet_daddr;
  758. dport = inet->inet_dport;
  759. /* Open fast path for connected socket.
  760. Route will not be used, if at least one option is set.
  761. */
  762. connected = 1;
  763. }
  764. ipc.addr = inet->inet_saddr;
  765. ipc.oif = sk->sk_bound_dev_if;
  766. err = sock_tx_timestamp(msg, sk, &ipc.shtx);
  767. if (err)
  768. return err;
  769. if (msg->msg_controllen) {
  770. err = ip_cmsg_send(sock_net(sk), msg, &ipc);
  771. if (err)
  772. return err;
  773. if (ipc.opt)
  774. free = 1;
  775. connected = 0;
  776. }
  777. if (!ipc.opt)
  778. ipc.opt = inet->opt;
  779. saddr = ipc.addr;
  780. ipc.addr = faddr = daddr;
  781. if (ipc.opt && ipc.opt->srr) {
  782. if (!daddr)
  783. return -EINVAL;
  784. faddr = ipc.opt->faddr;
  785. connected = 0;
  786. }
  787. tos = RT_TOS(inet->tos);
  788. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  789. (msg->msg_flags & MSG_DONTROUTE) ||
  790. (ipc.opt && ipc.opt->is_strictroute)) {
  791. tos |= RTO_ONLINK;
  792. connected = 0;
  793. }
  794. if (ipv4_is_multicast(daddr)) {
  795. if (!ipc.oif)
  796. ipc.oif = inet->mc_index;
  797. if (!saddr)
  798. saddr = inet->mc_addr;
  799. connected = 0;
  800. }
  801. if (connected)
  802. rt = (struct rtable *)sk_dst_check(sk, 0);
  803. if (rt == NULL) {
  804. struct flowi fl = { .oif = ipc.oif,
  805. .mark = sk->sk_mark,
  806. .nl_u = { .ip4_u =
  807. { .daddr = faddr,
  808. .saddr = saddr,
  809. .tos = tos } },
  810. .proto = sk->sk_protocol,
  811. .flags = inet_sk_flowi_flags(sk),
  812. .uli_u = { .ports =
  813. { .sport = inet->inet_sport,
  814. .dport = dport } } };
  815. struct net *net = sock_net(sk);
  816. security_sk_classify_flow(sk, &fl);
  817. err = ip_route_output_flow(net, &rt, &fl, sk, 1);
  818. if (err) {
  819. if (err == -ENETUNREACH)
  820. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  821. goto out;
  822. }
  823. err = -EACCES;
  824. if ((rt->rt_flags & RTCF_BROADCAST) &&
  825. !sock_flag(sk, SOCK_BROADCAST))
  826. goto out;
  827. if (connected)
  828. sk_dst_set(sk, dst_clone(&rt->u.dst));
  829. }
  830. if (msg->msg_flags&MSG_CONFIRM)
  831. goto do_confirm;
  832. back_from_confirm:
  833. saddr = rt->rt_src;
  834. if (!ipc.addr)
  835. daddr = ipc.addr = rt->rt_dst;
  836. lock_sock(sk);
  837. if (unlikely(up->pending)) {
  838. /* The socket is already corked while preparing it. */
  839. /* ... which is an evident application bug. --ANK */
  840. release_sock(sk);
  841. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  842. err = -EINVAL;
  843. goto out;
  844. }
  845. /*
  846. * Now cork the socket to pend data.
  847. */
  848. inet->cork.fl.fl4_dst = daddr;
  849. inet->cork.fl.fl_ip_dport = dport;
  850. inet->cork.fl.fl4_src = saddr;
  851. inet->cork.fl.fl_ip_sport = inet->inet_sport;
  852. up->pending = AF_INET;
  853. do_append_data:
  854. up->len += ulen;
  855. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  856. err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
  857. sizeof(struct udphdr), &ipc, &rt,
  858. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  859. if (err)
  860. udp_flush_pending_frames(sk);
  861. else if (!corkreq)
  862. err = udp_push_pending_frames(sk);
  863. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  864. up->pending = 0;
  865. release_sock(sk);
  866. out:
  867. ip_rt_put(rt);
  868. if (free)
  869. kfree(ipc.opt);
  870. if (!err)
  871. return len;
  872. /*
  873. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  874. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  875. * we don't have a good statistic (IpOutDiscards but it can be too many
  876. * things). We could add another new stat but at least for now that
  877. * seems like overkill.
  878. */
  879. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  880. UDP_INC_STATS_USER(sock_net(sk),
  881. UDP_MIB_SNDBUFERRORS, is_udplite);
  882. }
  883. return err;
  884. do_confirm:
  885. dst_confirm(&rt->u.dst);
  886. if (!(msg->msg_flags&MSG_PROBE) || len)
  887. goto back_from_confirm;
  888. err = 0;
  889. goto out;
  890. }
  891. EXPORT_SYMBOL(udp_sendmsg);
  892. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  893. size_t size, int flags)
  894. {
  895. struct udp_sock *up = udp_sk(sk);
  896. int ret;
  897. if (!up->pending) {
  898. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  899. /* Call udp_sendmsg to specify destination address which
  900. * sendpage interface can't pass.
  901. * This will succeed only when the socket is connected.
  902. */
  903. ret = udp_sendmsg(NULL, sk, &msg, 0);
  904. if (ret < 0)
  905. return ret;
  906. }
  907. lock_sock(sk);
  908. if (unlikely(!up->pending)) {
  909. release_sock(sk);
  910. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
  911. return -EINVAL;
  912. }
  913. ret = ip_append_page(sk, page, offset, size, flags);
  914. if (ret == -EOPNOTSUPP) {
  915. release_sock(sk);
  916. return sock_no_sendpage(sk->sk_socket, page, offset,
  917. size, flags);
  918. }
  919. if (ret < 0) {
  920. udp_flush_pending_frames(sk);
  921. goto out;
  922. }
  923. up->len += size;
  924. if (!(up->corkflag || (flags&MSG_MORE)))
  925. ret = udp_push_pending_frames(sk);
  926. if (!ret)
  927. ret = size;
  928. out:
  929. release_sock(sk);
  930. return ret;
  931. }
  932. /**
  933. * first_packet_length - return length of first packet in receive queue
  934. * @sk: socket
  935. *
  936. * Drops all bad checksum frames, until a valid one is found.
  937. * Returns the length of found skb, or 0 if none is found.
  938. */
  939. static unsigned int first_packet_length(struct sock *sk)
  940. {
  941. struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
  942. struct sk_buff *skb;
  943. unsigned int res;
  944. __skb_queue_head_init(&list_kill);
  945. spin_lock_bh(&rcvq->lock);
  946. while ((skb = skb_peek(rcvq)) != NULL &&
  947. udp_lib_checksum_complete(skb)) {
  948. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
  949. IS_UDPLITE(sk));
  950. atomic_inc(&sk->sk_drops);
  951. __skb_unlink(skb, rcvq);
  952. __skb_queue_tail(&list_kill, skb);
  953. }
  954. res = skb ? skb->len : 0;
  955. spin_unlock_bh(&rcvq->lock);
  956. if (!skb_queue_empty(&list_kill)) {
  957. lock_sock(sk);
  958. __skb_queue_purge(&list_kill);
  959. sk_mem_reclaim_partial(sk);
  960. release_sock(sk);
  961. }
  962. return res;
  963. }
  964. /*
  965. * IOCTL requests applicable to the UDP protocol
  966. */
  967. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  968. {
  969. switch (cmd) {
  970. case SIOCOUTQ:
  971. {
  972. int amount = sk_wmem_alloc_get(sk);
  973. return put_user(amount, (int __user *)arg);
  974. }
  975. case SIOCINQ:
  976. {
  977. unsigned int amount = first_packet_length(sk);
  978. if (amount)
  979. /*
  980. * We will only return the amount
  981. * of this packet since that is all
  982. * that will be read.
  983. */
  984. amount -= sizeof(struct udphdr);
  985. return put_user(amount, (int __user *)arg);
  986. }
  987. default:
  988. return -ENOIOCTLCMD;
  989. }
  990. return 0;
  991. }
  992. EXPORT_SYMBOL(udp_ioctl);
  993. /*
  994. * This should be easy, if there is something there we
  995. * return it, otherwise we block.
  996. */
  997. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  998. size_t len, int noblock, int flags, int *addr_len)
  999. {
  1000. struct inet_sock *inet = inet_sk(sk);
  1001. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  1002. struct sk_buff *skb;
  1003. unsigned int ulen, copied;
  1004. int peeked;
  1005. int err;
  1006. int is_udplite = IS_UDPLITE(sk);
  1007. /*
  1008. * Check any passed addresses
  1009. */
  1010. if (addr_len)
  1011. *addr_len = sizeof(*sin);
  1012. if (flags & MSG_ERRQUEUE)
  1013. return ip_recv_error(sk, msg, len);
  1014. try_again:
  1015. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  1016. &peeked, &err);
  1017. if (!skb)
  1018. goto out;
  1019. ulen = skb->len - sizeof(struct udphdr);
  1020. copied = len;
  1021. if (copied > ulen)
  1022. copied = ulen;
  1023. else if (copied < ulen)
  1024. msg->msg_flags |= MSG_TRUNC;
  1025. /*
  1026. * If checksum is needed at all, try to do it while copying the
  1027. * data. If the data is truncated, or if we only want a partial
  1028. * coverage checksum (UDP-Lite), do it before the copy.
  1029. */
  1030. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  1031. if (udp_lib_checksum_complete(skb))
  1032. goto csum_copy_err;
  1033. }
  1034. if (skb_csum_unnecessary(skb))
  1035. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  1036. msg->msg_iov, copied);
  1037. else {
  1038. err = skb_copy_and_csum_datagram_iovec(skb,
  1039. sizeof(struct udphdr),
  1040. msg->msg_iov);
  1041. if (err == -EINVAL)
  1042. goto csum_copy_err;
  1043. }
  1044. if (err)
  1045. goto out_free;
  1046. if (!peeked)
  1047. UDP_INC_STATS_USER(sock_net(sk),
  1048. UDP_MIB_INDATAGRAMS, is_udplite);
  1049. sock_recv_ts_and_drops(msg, sk, skb);
  1050. /* Copy the address. */
  1051. if (sin) {
  1052. sin->sin_family = AF_INET;
  1053. sin->sin_port = udp_hdr(skb)->source;
  1054. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  1055. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  1056. }
  1057. if (inet->cmsg_flags)
  1058. ip_cmsg_recv(msg, skb);
  1059. err = copied;
  1060. if (flags & MSG_TRUNC)
  1061. err = ulen;
  1062. out_free:
  1063. skb_free_datagram_locked(sk, skb);
  1064. out:
  1065. return err;
  1066. csum_copy_err:
  1067. lock_sock(sk);
  1068. if (!skb_kill_datagram(sk, skb, flags))
  1069. UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1070. release_sock(sk);
  1071. if (noblock)
  1072. return -EAGAIN;
  1073. goto try_again;
  1074. }
  1075. int udp_disconnect(struct sock *sk, int flags)
  1076. {
  1077. struct inet_sock *inet = inet_sk(sk);
  1078. /*
  1079. * 1003.1g - break association.
  1080. */
  1081. sk->sk_state = TCP_CLOSE;
  1082. inet->inet_daddr = 0;
  1083. inet->inet_dport = 0;
  1084. sk->sk_bound_dev_if = 0;
  1085. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  1086. inet_reset_saddr(sk);
  1087. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  1088. sk->sk_prot->unhash(sk);
  1089. inet->inet_sport = 0;
  1090. }
  1091. sk_dst_reset(sk);
  1092. return 0;
  1093. }
  1094. EXPORT_SYMBOL(udp_disconnect);
  1095. void udp_lib_unhash(struct sock *sk)
  1096. {
  1097. if (sk_hashed(sk)) {
  1098. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  1099. struct udp_hslot *hslot, *hslot2;
  1100. hslot = udp_hashslot(udptable, sock_net(sk),
  1101. udp_sk(sk)->udp_port_hash);
  1102. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  1103. spin_lock_bh(&hslot->lock);
  1104. if (sk_nulls_del_node_init_rcu(sk)) {
  1105. hslot->count--;
  1106. inet_sk(sk)->inet_num = 0;
  1107. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  1108. spin_lock(&hslot2->lock);
  1109. hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
  1110. hslot2->count--;
  1111. spin_unlock(&hslot2->lock);
  1112. }
  1113. spin_unlock_bh(&hslot->lock);
  1114. }
  1115. }
  1116. EXPORT_SYMBOL(udp_lib_unhash);
  1117. static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1118. {
  1119. int rc = sock_queue_rcv_skb(sk, skb);
  1120. if (rc < 0) {
  1121. int is_udplite = IS_UDPLITE(sk);
  1122. /* Note that an ENOMEM error is charged twice */
  1123. if (rc == -ENOMEM)
  1124. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  1125. is_udplite);
  1126. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1127. kfree_skb(skb);
  1128. return -1;
  1129. }
  1130. return 0;
  1131. }
  1132. /* returns:
  1133. * -1: error
  1134. * 0: success
  1135. * >0: "udp encap" protocol resubmission
  1136. *
  1137. * Note that in the success and error cases, the skb is assumed to
  1138. * have either been requeued or freed.
  1139. */
  1140. int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1141. {
  1142. struct udp_sock *up = udp_sk(sk);
  1143. int rc;
  1144. int is_udplite = IS_UDPLITE(sk);
  1145. /*
  1146. * Charge it to the socket, dropping if the queue is full.
  1147. */
  1148. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  1149. goto drop;
  1150. nf_reset(skb);
  1151. if (up->encap_type) {
  1152. /*
  1153. * This is an encapsulation socket so pass the skb to
  1154. * the socket's udp_encap_rcv() hook. Otherwise, just
  1155. * fall through and pass this up the UDP socket.
  1156. * up->encap_rcv() returns the following value:
  1157. * =0 if skb was successfully passed to the encap
  1158. * handler or was discarded by it.
  1159. * >0 if skb should be passed on to UDP.
  1160. * <0 if skb should be resubmitted as proto -N
  1161. */
  1162. /* if we're overly short, let UDP handle it */
  1163. if (skb->len > sizeof(struct udphdr) &&
  1164. up->encap_rcv != NULL) {
  1165. int ret;
  1166. ret = (*up->encap_rcv)(sk, skb);
  1167. if (ret <= 0) {
  1168. UDP_INC_STATS_BH(sock_net(sk),
  1169. UDP_MIB_INDATAGRAMS,
  1170. is_udplite);
  1171. return -ret;
  1172. }
  1173. }
  1174. /* FALLTHROUGH -- it's a UDP Packet */
  1175. }
  1176. /*
  1177. * UDP-Lite specific tests, ignored on UDP sockets
  1178. */
  1179. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  1180. /*
  1181. * MIB statistics other than incrementing the error count are
  1182. * disabled for the following two types of errors: these depend
  1183. * on the application settings, not on the functioning of the
  1184. * protocol stack as such.
  1185. *
  1186. * RFC 3828 here recommends (sec 3.3): "There should also be a
  1187. * way ... to ... at least let the receiving application block
  1188. * delivery of packets with coverage values less than a value
  1189. * provided by the application."
  1190. */
  1191. if (up->pcrlen == 0) { /* full coverage was set */
  1192. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  1193. "%d while full coverage %d requested\n",
  1194. UDP_SKB_CB(skb)->cscov, skb->len);
  1195. goto drop;
  1196. }
  1197. /* The next case involves violating the min. coverage requested
  1198. * by the receiver. This is subtle: if receiver wants x and x is
  1199. * greater than the buffersize/MTU then receiver will complain
  1200. * that it wants x while sender emits packets of smaller size y.
  1201. * Therefore the above ...()->partial_cov statement is essential.
  1202. */
  1203. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  1204. LIMIT_NETDEBUG(KERN_WARNING
  1205. "UDPLITE: coverage %d too small, need min %d\n",
  1206. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  1207. goto drop;
  1208. }
  1209. }
  1210. if (sk->sk_filter) {
  1211. if (udp_lib_checksum_complete(skb))
  1212. goto drop;
  1213. }
  1214. rc = 0;
  1215. bh_lock_sock(sk);
  1216. if (!sock_owned_by_user(sk))
  1217. rc = __udp_queue_rcv_skb(sk, skb);
  1218. else
  1219. sk_add_backlog(sk, skb);
  1220. bh_unlock_sock(sk);
  1221. return rc;
  1222. drop:
  1223. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1224. atomic_inc(&sk->sk_drops);
  1225. kfree_skb(skb);
  1226. return -1;
  1227. }
  1228. static void flush_stack(struct sock **stack, unsigned int count,
  1229. struct sk_buff *skb, unsigned int final)
  1230. {
  1231. unsigned int i;
  1232. struct sk_buff *skb1 = NULL;
  1233. struct sock *sk;
  1234. for (i = 0; i < count; i++) {
  1235. sk = stack[i];
  1236. if (likely(skb1 == NULL))
  1237. skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
  1238. if (!skb1) {
  1239. atomic_inc(&sk->sk_drops);
  1240. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  1241. IS_UDPLITE(sk));
  1242. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
  1243. IS_UDPLITE(sk));
  1244. }
  1245. if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0)
  1246. skb1 = NULL;
  1247. }
  1248. if (unlikely(skb1))
  1249. kfree_skb(skb1);
  1250. }
  1251. /*
  1252. * Multicasts and broadcasts go to each listener.
  1253. *
  1254. * Note: called only from the BH handler context.
  1255. */
  1256. static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  1257. struct udphdr *uh,
  1258. __be32 saddr, __be32 daddr,
  1259. struct udp_table *udptable)
  1260. {
  1261. struct sock *sk, *stack[256 / sizeof(struct sock *)];
  1262. struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
  1263. int dif;
  1264. unsigned int i, count = 0;
  1265. spin_lock(&hslot->lock);
  1266. sk = sk_nulls_head(&hslot->head);
  1267. dif = skb->dev->ifindex;
  1268. sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
  1269. while (sk) {
  1270. stack[count++] = sk;
  1271. sk = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
  1272. daddr, uh->source, saddr, dif);
  1273. if (unlikely(count == ARRAY_SIZE(stack))) {
  1274. if (!sk)
  1275. break;
  1276. flush_stack(stack, count, skb, ~0);
  1277. count = 0;
  1278. }
  1279. }
  1280. /*
  1281. * before releasing chain lock, we must take a reference on sockets
  1282. */
  1283. for (i = 0; i < count; i++)
  1284. sock_hold(stack[i]);
  1285. spin_unlock(&hslot->lock);
  1286. /*
  1287. * do the slow work with no lock held
  1288. */
  1289. if (count) {
  1290. flush_stack(stack, count, skb, count - 1);
  1291. for (i = 0; i < count; i++)
  1292. sock_put(stack[i]);
  1293. } else {
  1294. kfree_skb(skb);
  1295. }
  1296. return 0;
  1297. }
  1298. /* Initialize UDP checksum. If exited with zero value (success),
  1299. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  1300. * Otherwise, csum completion requires chacksumming packet body,
  1301. * including udp header and folding it to skb->csum.
  1302. */
  1303. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  1304. int proto)
  1305. {
  1306. const struct iphdr *iph;
  1307. int err;
  1308. UDP_SKB_CB(skb)->partial_cov = 0;
  1309. UDP_SKB_CB(skb)->cscov = skb->len;
  1310. if (proto == IPPROTO_UDPLITE) {
  1311. err = udplite_checksum_init(skb, uh);
  1312. if (err)
  1313. return err;
  1314. }
  1315. iph = ip_hdr(skb);
  1316. if (uh->check == 0) {
  1317. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1318. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1319. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1320. proto, skb->csum))
  1321. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1322. }
  1323. if (!skb_csum_unnecessary(skb))
  1324. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  1325. skb->len, proto, 0);
  1326. /* Probably, we should checksum udp header (it should be in cache
  1327. * in any case) and data in tiny packets (< rx copybreak).
  1328. */
  1329. return 0;
  1330. }
  1331. /*
  1332. * All we need to do is get the socket, and then do a checksum.
  1333. */
  1334. int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  1335. int proto)
  1336. {
  1337. struct sock *sk;
  1338. struct udphdr *uh;
  1339. unsigned short ulen;
  1340. struct rtable *rt = skb_rtable(skb);
  1341. __be32 saddr, daddr;
  1342. struct net *net = dev_net(skb->dev);
  1343. /*
  1344. * Validate the packet.
  1345. */
  1346. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1347. goto drop; /* No space for header. */
  1348. uh = udp_hdr(skb);
  1349. ulen = ntohs(uh->len);
  1350. if (ulen > skb->len)
  1351. goto short_packet;
  1352. if (proto == IPPROTO_UDP) {
  1353. /* UDP validates ulen. */
  1354. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1355. goto short_packet;
  1356. uh = udp_hdr(skb);
  1357. }
  1358. if (udp4_csum_init(skb, uh, proto))
  1359. goto csum_error;
  1360. saddr = ip_hdr(skb)->saddr;
  1361. daddr = ip_hdr(skb)->daddr;
  1362. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1363. return __udp4_lib_mcast_deliver(net, skb, uh,
  1364. saddr, daddr, udptable);
  1365. sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  1366. if (sk != NULL) {
  1367. int ret = udp_queue_rcv_skb(sk, skb);
  1368. sock_put(sk);
  1369. /* a return value > 0 means to resubmit the input, but
  1370. * it wants the return to be -protocol, or 0
  1371. */
  1372. if (ret > 0)
  1373. return -ret;
  1374. return 0;
  1375. }
  1376. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1377. goto drop;
  1378. nf_reset(skb);
  1379. /* No socket. Drop packet silently, if checksum is wrong */
  1380. if (udp_lib_checksum_complete(skb))
  1381. goto csum_error;
  1382. UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1383. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1384. /*
  1385. * Hmm. We got an UDP packet to a port to which we
  1386. * don't wanna listen. Ignore it.
  1387. */
  1388. kfree_skb(skb);
  1389. return 0;
  1390. short_packet:
  1391. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
  1392. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1393. &saddr,
  1394. ntohs(uh->source),
  1395. ulen,
  1396. skb->len,
  1397. &daddr,
  1398. ntohs(uh->dest));
  1399. goto drop;
  1400. csum_error:
  1401. /*
  1402. * RFC1122: OK. Discards the bad packet silently (as far as
  1403. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1404. */
  1405. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
  1406. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1407. &saddr,
  1408. ntohs(uh->source),
  1409. &daddr,
  1410. ntohs(uh->dest),
  1411. ulen);
  1412. drop:
  1413. UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1414. kfree_skb(skb);
  1415. return 0;
  1416. }
  1417. int udp_rcv(struct sk_buff *skb)
  1418. {
  1419. return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  1420. }
  1421. void udp_destroy_sock(struct sock *sk)
  1422. {
  1423. lock_sock(sk);
  1424. udp_flush_pending_frames(sk);
  1425. release_sock(sk);
  1426. }
  1427. /*
  1428. * Socket option code for UDP
  1429. */
  1430. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1431. char __user *optval, unsigned int optlen,
  1432. int (*push_pending_frames)(struct sock *))
  1433. {
  1434. struct udp_sock *up = udp_sk(sk);
  1435. int val;
  1436. int err = 0;
  1437. int is_udplite = IS_UDPLITE(sk);
  1438. if (optlen < sizeof(int))
  1439. return -EINVAL;
  1440. if (get_user(val, (int __user *)optval))
  1441. return -EFAULT;
  1442. switch (optname) {
  1443. case UDP_CORK:
  1444. if (val != 0) {
  1445. up->corkflag = 1;
  1446. } else {
  1447. up->corkflag = 0;
  1448. lock_sock(sk);
  1449. (*push_pending_frames)(sk);
  1450. release_sock(sk);
  1451. }
  1452. break;
  1453. case UDP_ENCAP:
  1454. switch (val) {
  1455. case 0:
  1456. case UDP_ENCAP_ESPINUDP:
  1457. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1458. up->encap_rcv = xfrm4_udp_encap_rcv;
  1459. /* FALLTHROUGH */
  1460. case UDP_ENCAP_L2TPINUDP:
  1461. up->encap_type = val;
  1462. break;
  1463. default:
  1464. err = -ENOPROTOOPT;
  1465. break;
  1466. }
  1467. break;
  1468. /*
  1469. * UDP-Lite's partial checksum coverage (RFC 3828).
  1470. */
  1471. /* The sender sets actual checksum coverage length via this option.
  1472. * The case coverage > packet length is handled by send module. */
  1473. case UDPLITE_SEND_CSCOV:
  1474. if (!is_udplite) /* Disable the option on UDP sockets */
  1475. return -ENOPROTOOPT;
  1476. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1477. val = 8;
  1478. else if (val > USHORT_MAX)
  1479. val = USHORT_MAX;
  1480. up->pcslen = val;
  1481. up->pcflag |= UDPLITE_SEND_CC;
  1482. break;
  1483. /* The receiver specifies a minimum checksum coverage value. To make
  1484. * sense, this should be set to at least 8 (as done below). If zero is
  1485. * used, this again means full checksum coverage. */
  1486. case UDPLITE_RECV_CSCOV:
  1487. if (!is_udplite) /* Disable the option on UDP sockets */
  1488. return -ENOPROTOOPT;
  1489. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1490. val = 8;
  1491. else if (val > USHORT_MAX)
  1492. val = USHORT_MAX;
  1493. up->pcrlen = val;
  1494. up->pcflag |= UDPLITE_RECV_CC;
  1495. break;
  1496. default:
  1497. err = -ENOPROTOOPT;
  1498. break;
  1499. }
  1500. return err;
  1501. }
  1502. EXPORT_SYMBOL(udp_lib_setsockopt);
  1503. int udp_setsockopt(struct sock *sk, int level, int optname,
  1504. char __user *optval, unsigned int optlen)
  1505. {
  1506. if (level == SOL_UDP || level == SOL_UDPLITE)
  1507. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1508. udp_push_pending_frames);
  1509. return ip_setsockopt(sk, level, optname, optval, optlen);
  1510. }
  1511. #ifdef CONFIG_COMPAT
  1512. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1513. char __user *optval, unsigned int optlen)
  1514. {
  1515. if (level == SOL_UDP || level == SOL_UDPLITE)
  1516. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1517. udp_push_pending_frames);
  1518. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1519. }
  1520. #endif
  1521. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1522. char __user *optval, int __user *optlen)
  1523. {
  1524. struct udp_sock *up = udp_sk(sk);
  1525. int val, len;
  1526. if (get_user(len, optlen))
  1527. return -EFAULT;
  1528. len = min_t(unsigned int, len, sizeof(int));
  1529. if (len < 0)
  1530. return -EINVAL;
  1531. switch (optname) {
  1532. case UDP_CORK:
  1533. val = up->corkflag;
  1534. break;
  1535. case UDP_ENCAP:
  1536. val = up->encap_type;
  1537. break;
  1538. /* The following two cannot be changed on UDP sockets, the return is
  1539. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1540. case UDPLITE_SEND_CSCOV:
  1541. val = up->pcslen;
  1542. break;
  1543. case UDPLITE_RECV_CSCOV:
  1544. val = up->pcrlen;
  1545. break;
  1546. default:
  1547. return -ENOPROTOOPT;
  1548. }
  1549. if (put_user(len, optlen))
  1550. return -EFAULT;
  1551. if (copy_to_user(optval, &val, len))
  1552. return -EFAULT;
  1553. return 0;
  1554. }
  1555. EXPORT_SYMBOL(udp_lib_getsockopt);
  1556. int udp_getsockopt(struct sock *sk, int level, int optname,
  1557. char __user *optval, int __user *optlen)
  1558. {
  1559. if (level == SOL_UDP || level == SOL_UDPLITE)
  1560. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1561. return ip_getsockopt(sk, level, optname, optval, optlen);
  1562. }
  1563. #ifdef CONFIG_COMPAT
  1564. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1565. char __user *optval, int __user *optlen)
  1566. {
  1567. if (level == SOL_UDP || level == SOL_UDPLITE)
  1568. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1569. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1570. }
  1571. #endif
  1572. /**
  1573. * udp_poll - wait for a UDP event.
  1574. * @file - file struct
  1575. * @sock - socket
  1576. * @wait - poll table
  1577. *
  1578. * This is same as datagram poll, except for the special case of
  1579. * blocking sockets. If application is using a blocking fd
  1580. * and a packet with checksum error is in the queue;
  1581. * then it could get return from select indicating data available
  1582. * but then block when reading it. Add special case code
  1583. * to work around these arguably broken applications.
  1584. */
  1585. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1586. {
  1587. unsigned int mask = datagram_poll(file, sock, wait);
  1588. struct sock *sk = sock->sk;
  1589. /* Check for false positives due to checksum errors */
  1590. if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
  1591. !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
  1592. mask &= ~(POLLIN | POLLRDNORM);
  1593. return mask;
  1594. }
  1595. EXPORT_SYMBOL(udp_poll);
  1596. struct proto udp_prot = {
  1597. .name = "UDP",
  1598. .owner = THIS_MODULE,
  1599. .close = udp_lib_close,
  1600. .connect = ip4_datagram_connect,
  1601. .disconnect = udp_disconnect,
  1602. .ioctl = udp_ioctl,
  1603. .destroy = udp_destroy_sock,
  1604. .setsockopt = udp_setsockopt,
  1605. .getsockopt = udp_getsockopt,
  1606. .sendmsg = udp_sendmsg,
  1607. .recvmsg = udp_recvmsg,
  1608. .sendpage = udp_sendpage,
  1609. .backlog_rcv = __udp_queue_rcv_skb,
  1610. .hash = udp_lib_hash,
  1611. .unhash = udp_lib_unhash,
  1612. .get_port = udp_v4_get_port,
  1613. .memory_allocated = &udp_memory_allocated,
  1614. .sysctl_mem = sysctl_udp_mem,
  1615. .sysctl_wmem = &sysctl_udp_wmem_min,
  1616. .sysctl_rmem = &sysctl_udp_rmem_min,
  1617. .obj_size = sizeof(struct udp_sock),
  1618. .slab_flags = SLAB_DESTROY_BY_RCU,
  1619. .h.udp_table = &udp_table,
  1620. #ifdef CONFIG_COMPAT
  1621. .compat_setsockopt = compat_udp_setsockopt,
  1622. .compat_getsockopt = compat_udp_getsockopt,
  1623. #endif
  1624. };
  1625. EXPORT_SYMBOL(udp_prot);
  1626. /* ------------------------------------------------------------------------ */
  1627. #ifdef CONFIG_PROC_FS
  1628. static struct sock *udp_get_first(struct seq_file *seq, int start)
  1629. {
  1630. struct sock *sk;
  1631. struct udp_iter_state *state = seq->private;
  1632. struct net *net = seq_file_net(seq);
  1633. for (state->bucket = start; state->bucket <= state->udp_table->mask;
  1634. ++state->bucket) {
  1635. struct hlist_nulls_node *node;
  1636. struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
  1637. if (hlist_nulls_empty(&hslot->head))
  1638. continue;
  1639. spin_lock_bh(&hslot->lock);
  1640. sk_nulls_for_each(sk, node, &hslot->head) {
  1641. if (!net_eq(sock_net(sk), net))
  1642. continue;
  1643. if (sk->sk_family == state->family)
  1644. goto found;
  1645. }
  1646. spin_unlock_bh(&hslot->lock);
  1647. }
  1648. sk = NULL;
  1649. found:
  1650. return sk;
  1651. }
  1652. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1653. {
  1654. struct udp_iter_state *state = seq->private;
  1655. struct net *net = seq_file_net(seq);
  1656. do {
  1657. sk = sk_nulls_next(sk);
  1658. } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
  1659. if (!sk) {
  1660. if (state->bucket <= state->udp_table->mask)
  1661. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  1662. return udp_get_first(seq, state->bucket + 1);
  1663. }
  1664. return sk;
  1665. }
  1666. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1667. {
  1668. struct sock *sk = udp_get_first(seq, 0);
  1669. if (sk)
  1670. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  1671. --pos;
  1672. return pos ? NULL : sk;
  1673. }
  1674. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1675. {
  1676. struct udp_iter_state *state = seq->private;
  1677. state->bucket = MAX_UDP_PORTS;
  1678. return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  1679. }
  1680. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1681. {
  1682. struct sock *sk;
  1683. if (v == SEQ_START_TOKEN)
  1684. sk = udp_get_idx(seq, 0);
  1685. else
  1686. sk = udp_get_next(seq, v);
  1687. ++*pos;
  1688. return sk;
  1689. }
  1690. static void udp_seq_stop(struct seq_file *seq, void *v)
  1691. {
  1692. struct udp_iter_state *state = seq->private;
  1693. if (state->bucket <= state->udp_table->mask)
  1694. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  1695. }
  1696. static int udp_seq_open(struct inode *inode, struct file *file)
  1697. {
  1698. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1699. struct udp_iter_state *s;
  1700. int err;
  1701. err = seq_open_net(inode, file, &afinfo->seq_ops,
  1702. sizeof(struct udp_iter_state));
  1703. if (err < 0)
  1704. return err;
  1705. s = ((struct seq_file *)file->private_data)->private;
  1706. s->family = afinfo->family;
  1707. s->udp_table = afinfo->udp_table;
  1708. return err;
  1709. }
  1710. /* ------------------------------------------------------------------------ */
  1711. int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
  1712. {
  1713. struct proc_dir_entry *p;
  1714. int rc = 0;
  1715. afinfo->seq_fops.open = udp_seq_open;
  1716. afinfo->seq_fops.read = seq_read;
  1717. afinfo->seq_fops.llseek = seq_lseek;
  1718. afinfo->seq_fops.release = seq_release_net;
  1719. afinfo->seq_ops.start = udp_seq_start;
  1720. afinfo->seq_ops.next = udp_seq_next;
  1721. afinfo->seq_ops.stop = udp_seq_stop;
  1722. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  1723. &afinfo->seq_fops, afinfo);
  1724. if (!p)
  1725. rc = -ENOMEM;
  1726. return rc;
  1727. }
  1728. EXPORT_SYMBOL(udp_proc_register);
  1729. void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
  1730. {
  1731. proc_net_remove(net, afinfo->name);
  1732. }
  1733. EXPORT_SYMBOL(udp_proc_unregister);
  1734. /* ------------------------------------------------------------------------ */
  1735. static void udp4_format_sock(struct sock *sp, struct seq_file *f,
  1736. int bucket, int *len)
  1737. {
  1738. struct inet_sock *inet = inet_sk(sp);
  1739. __be32 dest = inet->inet_daddr;
  1740. __be32 src = inet->inet_rcv_saddr;
  1741. __u16 destp = ntohs(inet->inet_dport);
  1742. __u16 srcp = ntohs(inet->inet_sport);
  1743. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  1744. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
  1745. bucket, src, srcp, dest, destp, sp->sk_state,
  1746. sk_wmem_alloc_get(sp),
  1747. sk_rmem_alloc_get(sp),
  1748. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1749. atomic_read(&sp->sk_refcnt), sp,
  1750. atomic_read(&sp->sk_drops), len);
  1751. }
  1752. int udp4_seq_show(struct seq_file *seq, void *v)
  1753. {
  1754. if (v == SEQ_START_TOKEN)
  1755. seq_printf(seq, "%-127s\n",
  1756. " sl local_address rem_address st tx_queue "
  1757. "rx_queue tr tm->when retrnsmt uid timeout "
  1758. "inode ref pointer drops");
  1759. else {
  1760. struct udp_iter_state *state = seq->private;
  1761. int len;
  1762. udp4_format_sock(v, seq, state->bucket, &len);
  1763. seq_printf(seq, "%*s\n", 127 - len, "");
  1764. }
  1765. return 0;
  1766. }
  1767. /* ------------------------------------------------------------------------ */
  1768. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1769. .name = "udp",
  1770. .family = AF_INET,
  1771. .udp_table = &udp_table,
  1772. .seq_fops = {
  1773. .owner = THIS_MODULE,
  1774. },
  1775. .seq_ops = {
  1776. .show = udp4_seq_show,
  1777. },
  1778. };
  1779. static int udp4_proc_init_net(struct net *net)
  1780. {
  1781. return udp_proc_register(net, &udp4_seq_afinfo);
  1782. }
  1783. static void udp4_proc_exit_net(struct net *net)
  1784. {
  1785. udp_proc_unregister(net, &udp4_seq_afinfo);
  1786. }
  1787. static struct pernet_operations udp4_net_ops = {
  1788. .init = udp4_proc_init_net,
  1789. .exit = udp4_proc_exit_net,
  1790. };
  1791. int __init udp4_proc_init(void)
  1792. {
  1793. return register_pernet_subsys(&udp4_net_ops);
  1794. }
  1795. void udp4_proc_exit(void)
  1796. {
  1797. unregister_pernet_subsys(&udp4_net_ops);
  1798. }
  1799. #endif /* CONFIG_PROC_FS */
  1800. static __initdata unsigned long uhash_entries;
  1801. static int __init set_uhash_entries(char *str)
  1802. {
  1803. if (!str)
  1804. return 0;
  1805. uhash_entries = simple_strtoul(str, &str, 0);
  1806. if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
  1807. uhash_entries = UDP_HTABLE_SIZE_MIN;
  1808. return 1;
  1809. }
  1810. __setup("uhash_entries=", set_uhash_entries);
  1811. void __init udp_table_init(struct udp_table *table, const char *name)
  1812. {
  1813. unsigned int i;
  1814. if (!CONFIG_BASE_SMALL)
  1815. table->hash = alloc_large_system_hash(name,
  1816. 2 * sizeof(struct udp_hslot),
  1817. uhash_entries,
  1818. 21, /* one slot per 2 MB */
  1819. 0,
  1820. &table->log,
  1821. &table->mask,
  1822. 64 * 1024);
  1823. /*
  1824. * Make sure hash table has the minimum size
  1825. */
  1826. if (CONFIG_BASE_SMALL || table->mask < UDP_HTABLE_SIZE_MIN - 1) {
  1827. table->hash = kmalloc(UDP_HTABLE_SIZE_MIN *
  1828. 2 * sizeof(struct udp_hslot), GFP_KERNEL);
  1829. if (!table->hash)
  1830. panic(name);
  1831. table->log = ilog2(UDP_HTABLE_SIZE_MIN);
  1832. table->mask = UDP_HTABLE_SIZE_MIN - 1;
  1833. }
  1834. table->hash2 = table->hash + (table->mask + 1);
  1835. for (i = 0; i <= table->mask; i++) {
  1836. INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
  1837. table->hash[i].count = 0;
  1838. spin_lock_init(&table->hash[i].lock);
  1839. }
  1840. for (i = 0; i <= table->mask; i++) {
  1841. INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
  1842. table->hash2[i].count = 0;
  1843. spin_lock_init(&table->hash2[i].lock);
  1844. }
  1845. }
  1846. void __init udp_init(void)
  1847. {
  1848. unsigned long nr_pages, limit;
  1849. udp_table_init(&udp_table, "UDP");
  1850. /* Set the pressure threshold up by the same strategy of TCP. It is a
  1851. * fraction of global memory that is up to 1/2 at 256 MB, decreasing
  1852. * toward zero with the amount of memory, with a floor of 128 pages.
  1853. */
  1854. nr_pages = totalram_pages - totalhigh_pages;
  1855. limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
  1856. limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
  1857. limit = max(limit, 128UL);
  1858. sysctl_udp_mem[0] = limit / 4 * 3;
  1859. sysctl_udp_mem[1] = limit;
  1860. sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
  1861. sysctl_udp_rmem_min = SK_MEM_QUANTUM;
  1862. sysctl_udp_wmem_min = SK_MEM_QUANTUM;
  1863. }
  1864. int udp4_ufo_send_check(struct sk_buff *skb)
  1865. {
  1866. const struct iphdr *iph;
  1867. struct udphdr *uh;
  1868. if (!pskb_may_pull(skb, sizeof(*uh)))
  1869. return -EINVAL;
  1870. iph = ip_hdr(skb);
  1871. uh = udp_hdr(skb);
  1872. uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1873. IPPROTO_UDP, 0);
  1874. skb->csum_start = skb_transport_header(skb) - skb->head;
  1875. skb->csum_offset = offsetof(struct udphdr, check);
  1876. skb->ip_summed = CHECKSUM_PARTIAL;
  1877. return 0;
  1878. }
  1879. struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, int features)
  1880. {
  1881. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1882. unsigned int mss;
  1883. int offset;
  1884. __wsum csum;
  1885. mss = skb_shinfo(skb)->gso_size;
  1886. if (unlikely(skb->len <= mss))
  1887. goto out;
  1888. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  1889. /* Packet is from an untrusted source, reset gso_segs. */
  1890. int type = skb_shinfo(skb)->gso_type;
  1891. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
  1892. !(type & (SKB_GSO_UDP))))
  1893. goto out;
  1894. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  1895. segs = NULL;
  1896. goto out;
  1897. }
  1898. /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
  1899. * do checksum of UDP packets sent as multiple IP fragments.
  1900. */
  1901. offset = skb->csum_start - skb_headroom(skb);
  1902. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1903. offset += skb->csum_offset;
  1904. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1905. skb->ip_summed = CHECKSUM_NONE;
  1906. /* Fragment the skb. IP headers of the fragments are updated in
  1907. * inet_gso_segment()
  1908. */
  1909. segs = skb_segment(skb, features);
  1910. out:
  1911. return segs;
  1912. }