udp.c 38 KB

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