udp.c 57 KB

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