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