udp.c 43 KB

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