udp.c 43 KB

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