udp.c 44 KB

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