udp.c 45 KB

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