udp.c 45 KB

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