udp.c 45 KB

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