udp.c 45 KB

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