udp.c 43 KB

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