udp.c 43 KB

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