udp.c 43 KB

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