udp.c 43 KB

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