udp.c 38 KB

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