udp.c 37 KB

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