udp.c 57 KB

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