udp.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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/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. if (up->encap_type) {
  881. /*
  882. * This is an encapsulation socket, so let's see if this is
  883. * an encapsulated packet.
  884. * If it's a keepalive packet, then just eat it.
  885. * If it's an encapsulateed packet, then pass it to the
  886. * IPsec xfrm input and return the response
  887. * appropriately. Otherwise, just fall through and
  888. * pass this up the UDP socket.
  889. */
  890. int ret;
  891. ret = udp_encap_rcv(sk, skb);
  892. if (ret == 0) {
  893. /* Eat the packet .. */
  894. kfree_skb(skb);
  895. return 0;
  896. }
  897. if (ret < 0) {
  898. /* process the ESP packet */
  899. ret = xfrm4_rcv_encap(skb, up->encap_type);
  900. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
  901. return -ret;
  902. }
  903. /* FALLTHROUGH -- it's a UDP Packet */
  904. }
  905. if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
  906. if (__udp_checksum_complete(skb)) {
  907. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  908. kfree_skb(skb);
  909. return -1;
  910. }
  911. skb->ip_summed = CHECKSUM_UNNECESSARY;
  912. }
  913. if (sock_queue_rcv_skb(sk,skb)<0) {
  914. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  915. kfree_skb(skb);
  916. return -1;
  917. }
  918. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
  919. return 0;
  920. }
  921. /*
  922. * Multicasts and broadcasts go to each listener.
  923. *
  924. * Note: called only from the BH handler context,
  925. * so we don't need to lock the hashes.
  926. */
  927. static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
  928. u32 saddr, u32 daddr)
  929. {
  930. struct sock *sk;
  931. int dif;
  932. read_lock(&udp_hash_lock);
  933. sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
  934. dif = skb->dev->ifindex;
  935. sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
  936. if (sk) {
  937. struct sock *sknext = NULL;
  938. do {
  939. struct sk_buff *skb1 = skb;
  940. sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
  941. uh->source, saddr, dif);
  942. if(sknext)
  943. skb1 = skb_clone(skb, GFP_ATOMIC);
  944. if(skb1) {
  945. int ret = udp_queue_rcv_skb(sk, skb1);
  946. if (ret > 0)
  947. /* we should probably re-process instead
  948. * of dropping packets here. */
  949. kfree_skb(skb1);
  950. }
  951. sk = sknext;
  952. } while(sknext);
  953. } else
  954. kfree_skb(skb);
  955. read_unlock(&udp_hash_lock);
  956. return 0;
  957. }
  958. /* Initialize UDP checksum. If exited with zero value (success),
  959. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  960. * Otherwise, csum completion requires chacksumming packet body,
  961. * including udp header and folding it to skb->csum.
  962. */
  963. static void udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
  964. unsigned short ulen, u32 saddr, u32 daddr)
  965. {
  966. if (uh->check == 0) {
  967. skb->ip_summed = CHECKSUM_UNNECESSARY;
  968. } else if (skb->ip_summed == CHECKSUM_HW) {
  969. if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
  970. skb->ip_summed = CHECKSUM_UNNECESSARY;
  971. }
  972. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  973. skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  974. /* Probably, we should checksum udp header (it should be in cache
  975. * in any case) and data in tiny packets (< rx copybreak).
  976. */
  977. }
  978. /*
  979. * All we need to do is get the socket, and then do a checksum.
  980. */
  981. int udp_rcv(struct sk_buff *skb)
  982. {
  983. struct sock *sk;
  984. struct udphdr *uh;
  985. unsigned short ulen;
  986. struct rtable *rt = (struct rtable*)skb->dst;
  987. u32 saddr = skb->nh.iph->saddr;
  988. u32 daddr = skb->nh.iph->daddr;
  989. int len = skb->len;
  990. /*
  991. * Validate the packet and the UDP length.
  992. */
  993. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  994. goto no_header;
  995. uh = skb->h.uh;
  996. ulen = ntohs(uh->len);
  997. if (ulen > len || ulen < sizeof(*uh))
  998. goto short_packet;
  999. if (pskb_trim_rcsum(skb, ulen))
  1000. goto short_packet;
  1001. udp_checksum_init(skb, uh, ulen, saddr, daddr);
  1002. if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1003. return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
  1004. sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
  1005. if (sk != NULL) {
  1006. int ret = udp_queue_rcv_skb(sk, skb);
  1007. sock_put(sk);
  1008. /* a return value > 0 means to resubmit the input, but
  1009. * it it wants the return to be -protocol, or 0
  1010. */
  1011. if (ret > 0)
  1012. return -ret;
  1013. return 0;
  1014. }
  1015. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1016. goto drop;
  1017. /* No socket. Drop packet silently, if checksum is wrong */
  1018. if (udp_checksum_complete(skb))
  1019. goto csum_error;
  1020. UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
  1021. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1022. /*
  1023. * Hmm. We got an UDP packet to a port to which we
  1024. * don't wanna listen. Ignore it.
  1025. */
  1026. kfree_skb(skb);
  1027. return(0);
  1028. short_packet:
  1029. LIMIT_NETDEBUG(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
  1030. NIPQUAD(saddr),
  1031. ntohs(uh->source),
  1032. ulen,
  1033. len,
  1034. NIPQUAD(daddr),
  1035. ntohs(uh->dest));
  1036. no_header:
  1037. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1038. kfree_skb(skb);
  1039. return(0);
  1040. csum_error:
  1041. /*
  1042. * RFC1122: OK. Discards the bad packet silently (as far as
  1043. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1044. */
  1045. LIMIT_NETDEBUG(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
  1046. NIPQUAD(saddr),
  1047. ntohs(uh->source),
  1048. NIPQUAD(daddr),
  1049. ntohs(uh->dest),
  1050. ulen);
  1051. drop:
  1052. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1053. kfree_skb(skb);
  1054. return(0);
  1055. }
  1056. static int udp_destroy_sock(struct sock *sk)
  1057. {
  1058. lock_sock(sk);
  1059. udp_flush_pending_frames(sk);
  1060. release_sock(sk);
  1061. return 0;
  1062. }
  1063. /*
  1064. * Socket option code for UDP
  1065. */
  1066. static int udp_setsockopt(struct sock *sk, int level, int optname,
  1067. char __user *optval, int optlen)
  1068. {
  1069. struct udp_sock *up = udp_sk(sk);
  1070. int val;
  1071. int err = 0;
  1072. if (level != SOL_UDP)
  1073. return ip_setsockopt(sk, level, optname, optval, optlen);
  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_getsockopt(struct sock *sk, int level, int optname,
  1108. char __user *optval, int __user *optlen)
  1109. {
  1110. struct udp_sock *up = udp_sk(sk);
  1111. int val, len;
  1112. if (level != SOL_UDP)
  1113. return ip_getsockopt(sk, level, optname, optval, optlen);
  1114. if(get_user(len,optlen))
  1115. return -EFAULT;
  1116. len = min_t(unsigned int, len, sizeof(int));
  1117. if(len < 0)
  1118. return -EINVAL;
  1119. switch(optname) {
  1120. case UDP_CORK:
  1121. val = up->corkflag;
  1122. break;
  1123. case UDP_ENCAP:
  1124. val = up->encap_type;
  1125. break;
  1126. default:
  1127. return -ENOPROTOOPT;
  1128. };
  1129. if(put_user(len, optlen))
  1130. return -EFAULT;
  1131. if(copy_to_user(optval, &val,len))
  1132. return -EFAULT;
  1133. return 0;
  1134. }
  1135. /**
  1136. * udp_poll - wait for a UDP event.
  1137. * @file - file struct
  1138. * @sock - socket
  1139. * @wait - poll table
  1140. *
  1141. * This is same as datagram poll, except for the special case of
  1142. * blocking sockets. If application is using a blocking fd
  1143. * and a packet with checksum error is in the queue;
  1144. * then it could get return from select indicating data available
  1145. * but then block when reading it. Add special case code
  1146. * to work around these arguably broken applications.
  1147. */
  1148. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1149. {
  1150. unsigned int mask = datagram_poll(file, sock, wait);
  1151. struct sock *sk = sock->sk;
  1152. /* Check for false positives due to checksum errors */
  1153. if ( (mask & POLLRDNORM) &&
  1154. !(file->f_flags & O_NONBLOCK) &&
  1155. !(sk->sk_shutdown & RCV_SHUTDOWN)){
  1156. struct sk_buff_head *rcvq = &sk->sk_receive_queue;
  1157. struct sk_buff *skb;
  1158. spin_lock_bh(&rcvq->lock);
  1159. while ((skb = skb_peek(rcvq)) != NULL) {
  1160. if (udp_checksum_complete(skb)) {
  1161. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1162. __skb_unlink(skb, rcvq);
  1163. kfree_skb(skb);
  1164. } else {
  1165. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1166. break;
  1167. }
  1168. }
  1169. spin_unlock_bh(&rcvq->lock);
  1170. /* nothing to see, move along */
  1171. if (skb == NULL)
  1172. mask &= ~(POLLIN | POLLRDNORM);
  1173. }
  1174. return mask;
  1175. }
  1176. struct proto udp_prot = {
  1177. .name = "UDP",
  1178. .owner = THIS_MODULE,
  1179. .close = udp_close,
  1180. .connect = ip4_datagram_connect,
  1181. .disconnect = udp_disconnect,
  1182. .ioctl = udp_ioctl,
  1183. .destroy = udp_destroy_sock,
  1184. .setsockopt = udp_setsockopt,
  1185. .getsockopt = udp_getsockopt,
  1186. .sendmsg = udp_sendmsg,
  1187. .recvmsg = udp_recvmsg,
  1188. .sendpage = udp_sendpage,
  1189. .backlog_rcv = udp_queue_rcv_skb,
  1190. .hash = udp_v4_hash,
  1191. .unhash = udp_v4_unhash,
  1192. .get_port = udp_v4_get_port,
  1193. .obj_size = sizeof(struct udp_sock),
  1194. };
  1195. /* ------------------------------------------------------------------------ */
  1196. #ifdef CONFIG_PROC_FS
  1197. static struct sock *udp_get_first(struct seq_file *seq)
  1198. {
  1199. struct sock *sk;
  1200. struct udp_iter_state *state = seq->private;
  1201. for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
  1202. struct hlist_node *node;
  1203. sk_for_each(sk, node, &udp_hash[state->bucket]) {
  1204. if (sk->sk_family == state->family)
  1205. goto found;
  1206. }
  1207. }
  1208. sk = NULL;
  1209. found:
  1210. return sk;
  1211. }
  1212. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1213. {
  1214. struct udp_iter_state *state = seq->private;
  1215. do {
  1216. sk = sk_next(sk);
  1217. try_again:
  1218. ;
  1219. } while (sk && sk->sk_family != state->family);
  1220. if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
  1221. sk = sk_head(&udp_hash[state->bucket]);
  1222. goto try_again;
  1223. }
  1224. return sk;
  1225. }
  1226. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1227. {
  1228. struct sock *sk = udp_get_first(seq);
  1229. if (sk)
  1230. while(pos && (sk = udp_get_next(seq, sk)) != NULL)
  1231. --pos;
  1232. return pos ? NULL : sk;
  1233. }
  1234. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1235. {
  1236. read_lock(&udp_hash_lock);
  1237. return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
  1238. }
  1239. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1240. {
  1241. struct sock *sk;
  1242. if (v == (void *)1)
  1243. sk = udp_get_idx(seq, 0);
  1244. else
  1245. sk = udp_get_next(seq, v);
  1246. ++*pos;
  1247. return sk;
  1248. }
  1249. static void udp_seq_stop(struct seq_file *seq, void *v)
  1250. {
  1251. read_unlock(&udp_hash_lock);
  1252. }
  1253. static int udp_seq_open(struct inode *inode, struct file *file)
  1254. {
  1255. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1256. struct seq_file *seq;
  1257. int rc = -ENOMEM;
  1258. struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  1259. if (!s)
  1260. goto out;
  1261. memset(s, 0, sizeof(*s));
  1262. s->family = afinfo->family;
  1263. s->seq_ops.start = udp_seq_start;
  1264. s->seq_ops.next = udp_seq_next;
  1265. s->seq_ops.show = afinfo->seq_show;
  1266. s->seq_ops.stop = udp_seq_stop;
  1267. rc = seq_open(file, &s->seq_ops);
  1268. if (rc)
  1269. goto out_kfree;
  1270. seq = file->private_data;
  1271. seq->private = s;
  1272. out:
  1273. return rc;
  1274. out_kfree:
  1275. kfree(s);
  1276. goto out;
  1277. }
  1278. /* ------------------------------------------------------------------------ */
  1279. int udp_proc_register(struct udp_seq_afinfo *afinfo)
  1280. {
  1281. struct proc_dir_entry *p;
  1282. int rc = 0;
  1283. if (!afinfo)
  1284. return -EINVAL;
  1285. afinfo->seq_fops->owner = afinfo->owner;
  1286. afinfo->seq_fops->open = udp_seq_open;
  1287. afinfo->seq_fops->read = seq_read;
  1288. afinfo->seq_fops->llseek = seq_lseek;
  1289. afinfo->seq_fops->release = seq_release_private;
  1290. p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
  1291. if (p)
  1292. p->data = afinfo;
  1293. else
  1294. rc = -ENOMEM;
  1295. return rc;
  1296. }
  1297. void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
  1298. {
  1299. if (!afinfo)
  1300. return;
  1301. proc_net_remove(afinfo->name);
  1302. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1303. }
  1304. /* ------------------------------------------------------------------------ */
  1305. static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
  1306. {
  1307. struct inet_sock *inet = inet_sk(sp);
  1308. unsigned int dest = inet->daddr;
  1309. unsigned int src = inet->rcv_saddr;
  1310. __u16 destp = ntohs(inet->dport);
  1311. __u16 srcp = ntohs(inet->sport);
  1312. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1313. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
  1314. bucket, src, srcp, dest, destp, sp->sk_state,
  1315. atomic_read(&sp->sk_wmem_alloc),
  1316. atomic_read(&sp->sk_rmem_alloc),
  1317. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1318. atomic_read(&sp->sk_refcnt), sp);
  1319. }
  1320. static int udp4_seq_show(struct seq_file *seq, void *v)
  1321. {
  1322. if (v == SEQ_START_TOKEN)
  1323. seq_printf(seq, "%-127s\n",
  1324. " sl local_address rem_address st tx_queue "
  1325. "rx_queue tr tm->when retrnsmt uid timeout "
  1326. "inode");
  1327. else {
  1328. char tmpbuf[129];
  1329. struct udp_iter_state *state = seq->private;
  1330. udp4_format_sock(v, tmpbuf, state->bucket);
  1331. seq_printf(seq, "%-127s\n", tmpbuf);
  1332. }
  1333. return 0;
  1334. }
  1335. /* ------------------------------------------------------------------------ */
  1336. static struct file_operations udp4_seq_fops;
  1337. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1338. .owner = THIS_MODULE,
  1339. .name = "udp",
  1340. .family = AF_INET,
  1341. .seq_show = udp4_seq_show,
  1342. .seq_fops = &udp4_seq_fops,
  1343. };
  1344. int __init udp4_proc_init(void)
  1345. {
  1346. return udp_proc_register(&udp4_seq_afinfo);
  1347. }
  1348. void udp4_proc_exit(void)
  1349. {
  1350. udp_proc_unregister(&udp4_seq_afinfo);
  1351. }
  1352. #endif /* CONFIG_PROC_FS */
  1353. EXPORT_SYMBOL(udp_disconnect);
  1354. EXPORT_SYMBOL(udp_hash);
  1355. EXPORT_SYMBOL(udp_hash_lock);
  1356. EXPORT_SYMBOL(udp_ioctl);
  1357. EXPORT_SYMBOL(udp_port_rover);
  1358. EXPORT_SYMBOL(udp_prot);
  1359. EXPORT_SYMBOL(udp_sendmsg);
  1360. EXPORT_SYMBOL(udp_poll);
  1361. #ifdef CONFIG_PROC_FS
  1362. EXPORT_SYMBOL(udp_proc_register);
  1363. EXPORT_SYMBOL(udp_proc_unregister);
  1364. #endif