udp.c 39 KB

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