udp.c 43 KB

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