udp.c 57 KB

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