udp.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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 (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
  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. /* Clear queue. */
  759. if (flags&MSG_PEEK) {
  760. int clear = 0;
  761. spin_lock_bh(&sk->sk_receive_queue.lock);
  762. if (skb == skb_peek(&sk->sk_receive_queue)) {
  763. __skb_unlink(skb, &sk->sk_receive_queue);
  764. clear = 1;
  765. }
  766. spin_unlock_bh(&sk->sk_receive_queue.lock);
  767. if (clear)
  768. kfree_skb(skb);
  769. }
  770. skb_free_datagram(sk, skb);
  771. if (noblock)
  772. return -EAGAIN;
  773. goto try_again;
  774. }
  775. int udp_disconnect(struct sock *sk, int flags)
  776. {
  777. struct inet_sock *inet = inet_sk(sk);
  778. /*
  779. * 1003.1g - break association.
  780. */
  781. sk->sk_state = TCP_CLOSE;
  782. inet->daddr = 0;
  783. inet->dport = 0;
  784. sk->sk_bound_dev_if = 0;
  785. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  786. inet_reset_saddr(sk);
  787. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  788. sk->sk_prot->unhash(sk);
  789. inet->sport = 0;
  790. }
  791. sk_dst_reset(sk);
  792. return 0;
  793. }
  794. static void udp_close(struct sock *sk, long timeout)
  795. {
  796. sk_common_release(sk);
  797. }
  798. /* return:
  799. * 1 if the the UDP system should process it
  800. * 0 if we should drop this packet
  801. * -1 if it should get processed by xfrm4_rcv_encap
  802. */
  803. static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
  804. {
  805. #ifndef CONFIG_XFRM
  806. return 1;
  807. #else
  808. struct udp_sock *up = udp_sk(sk);
  809. struct udphdr *uh = skb->h.uh;
  810. struct iphdr *iph;
  811. int iphlen, len;
  812. __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
  813. __u32 *udpdata32 = (__u32 *)udpdata;
  814. __u16 encap_type = up->encap_type;
  815. /* if we're overly short, let UDP handle it */
  816. if (udpdata > skb->tail)
  817. return 1;
  818. /* if this is not encapsulated socket, then just return now */
  819. if (!encap_type)
  820. return 1;
  821. len = skb->tail - udpdata;
  822. switch (encap_type) {
  823. default:
  824. case UDP_ENCAP_ESPINUDP:
  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 > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
  829. /* ESP Packet without Non-ESP header */
  830. len = sizeof(struct udphdr);
  831. } else
  832. /* Must be an IKE packet.. pass it through */
  833. return 1;
  834. break;
  835. case UDP_ENCAP_ESPINUDP_NON_IKE:
  836. /* Check if this is a keepalive packet. If so, eat it. */
  837. if (len == 1 && udpdata[0] == 0xff) {
  838. return 0;
  839. } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
  840. udpdata32[0] == 0 && udpdata32[1] == 0) {
  841. /* ESP Packet with Non-IKE marker */
  842. len = sizeof(struct udphdr) + 2 * sizeof(u32);
  843. } else
  844. /* Must be an IKE packet.. pass it through */
  845. return 1;
  846. break;
  847. }
  848. /* At this point we are sure that this is an ESPinUDP packet,
  849. * so we need to remove 'len' bytes from the packet (the UDP
  850. * header and optional ESP marker bytes) and then modify the
  851. * protocol to ESP, and then call into the transform receiver.
  852. */
  853. if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  854. return 0;
  855. /* Now we can update and verify the packet length... */
  856. iph = skb->nh.iph;
  857. iphlen = iph->ihl << 2;
  858. iph->tot_len = htons(ntohs(iph->tot_len) - len);
  859. if (skb->len < iphlen + len) {
  860. /* packet is too small!?! */
  861. return 0;
  862. }
  863. /* pull the data buffer up to the ESP header and set the
  864. * transport header to point to ESP. Keep UDP on the stack
  865. * for later.
  866. */
  867. skb->h.raw = skb_pull(skb, len);
  868. /* modify the protocol (it's ESP!) */
  869. iph->protocol = IPPROTO_ESP;
  870. /* and let the caller know to send this into the ESP processor... */
  871. return -1;
  872. #endif
  873. }
  874. /* returns:
  875. * -1: error
  876. * 0: success
  877. * >0: "udp encap" protocol resubmission
  878. *
  879. * Note that in the success and error cases, the skb is assumed to
  880. * have either been requeued or freed.
  881. */
  882. static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
  883. {
  884. struct udp_sock *up = udp_sk(sk);
  885. /*
  886. * Charge it to the socket, dropping if the queue is full.
  887. */
  888. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
  889. kfree_skb(skb);
  890. return -1;
  891. }
  892. if (up->encap_type) {
  893. /*
  894. * This is an encapsulation socket, so let's see if this is
  895. * an encapsulated packet.
  896. * If it's a keepalive packet, then just eat it.
  897. * If it's an encapsulateed packet, then pass it to the
  898. * IPsec xfrm input and return the response
  899. * appropriately. Otherwise, just fall through and
  900. * pass this up the UDP socket.
  901. */
  902. int ret;
  903. ret = udp_encap_rcv(sk, skb);
  904. if (ret == 0) {
  905. /* Eat the packet .. */
  906. kfree_skb(skb);
  907. return 0;
  908. }
  909. if (ret < 0) {
  910. /* process the ESP packet */
  911. ret = xfrm4_rcv_encap(skb, up->encap_type);
  912. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
  913. return -ret;
  914. }
  915. /* FALLTHROUGH -- it's a UDP Packet */
  916. }
  917. if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
  918. if (__udp_checksum_complete(skb)) {
  919. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  920. kfree_skb(skb);
  921. return -1;
  922. }
  923. skb->ip_summed = CHECKSUM_UNNECESSARY;
  924. }
  925. if (sock_queue_rcv_skb(sk,skb)<0) {
  926. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  927. kfree_skb(skb);
  928. return -1;
  929. }
  930. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
  931. return 0;
  932. }
  933. /*
  934. * Multicasts and broadcasts go to each listener.
  935. *
  936. * Note: called only from the BH handler context,
  937. * so we don't need to lock the hashes.
  938. */
  939. static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
  940. u32 saddr, u32 daddr)
  941. {
  942. struct sock *sk;
  943. int dif;
  944. read_lock(&udp_hash_lock);
  945. sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
  946. dif = skb->dev->ifindex;
  947. sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
  948. if (sk) {
  949. struct sock *sknext = NULL;
  950. do {
  951. struct sk_buff *skb1 = skb;
  952. sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
  953. uh->source, saddr, dif);
  954. if(sknext)
  955. skb1 = skb_clone(skb, GFP_ATOMIC);
  956. if(skb1) {
  957. int ret = udp_queue_rcv_skb(sk, skb1);
  958. if (ret > 0)
  959. /* we should probably re-process instead
  960. * of dropping packets here. */
  961. kfree_skb(skb1);
  962. }
  963. sk = sknext;
  964. } while(sknext);
  965. } else
  966. kfree_skb(skb);
  967. read_unlock(&udp_hash_lock);
  968. return 0;
  969. }
  970. /* Initialize UDP checksum. If exited with zero value (success),
  971. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  972. * Otherwise, csum completion requires chacksumming packet body,
  973. * including udp header and folding it to skb->csum.
  974. */
  975. static int udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
  976. unsigned short ulen, u32 saddr, u32 daddr)
  977. {
  978. if (uh->check == 0) {
  979. skb->ip_summed = CHECKSUM_UNNECESSARY;
  980. } else if (skb->ip_summed == CHECKSUM_HW) {
  981. skb->ip_summed = CHECKSUM_UNNECESSARY;
  982. if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
  983. return 0;
  984. LIMIT_NETDEBUG(KERN_DEBUG "udp v4 hw csum failure.\n");
  985. skb->ip_summed = CHECKSUM_NONE;
  986. }
  987. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  988. skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  989. /* Probably, we should checksum udp header (it should be in cache
  990. * in any case) and data in tiny packets (< rx copybreak).
  991. */
  992. return 0;
  993. }
  994. /*
  995. * All we need to do is get the socket, and then do a checksum.
  996. */
  997. int udp_rcv(struct sk_buff *skb)
  998. {
  999. struct sock *sk;
  1000. struct udphdr *uh;
  1001. unsigned short ulen;
  1002. struct rtable *rt = (struct rtable*)skb->dst;
  1003. u32 saddr = skb->nh.iph->saddr;
  1004. u32 daddr = skb->nh.iph->daddr;
  1005. int len = skb->len;
  1006. /*
  1007. * Validate the packet and the UDP length.
  1008. */
  1009. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1010. goto no_header;
  1011. uh = skb->h.uh;
  1012. ulen = ntohs(uh->len);
  1013. if (ulen > len || ulen < sizeof(*uh))
  1014. goto short_packet;
  1015. if (pskb_trim_rcsum(skb, ulen))
  1016. goto short_packet;
  1017. if (udp_checksum_init(skb, uh, ulen, saddr, daddr) < 0)
  1018. goto csum_error;
  1019. if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1020. return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
  1021. sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
  1022. if (sk != NULL) {
  1023. int ret = udp_queue_rcv_skb(sk, skb);
  1024. sock_put(sk);
  1025. /* a return value > 0 means to resubmit the input, but
  1026. * it it wants the return to be -protocol, or 0
  1027. */
  1028. if (ret > 0)
  1029. return -ret;
  1030. return 0;
  1031. }
  1032. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1033. goto drop;
  1034. /* No socket. Drop packet silently, if checksum is wrong */
  1035. if (udp_checksum_complete(skb))
  1036. goto csum_error;
  1037. UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
  1038. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1039. /*
  1040. * Hmm. We got an UDP packet to a port to which we
  1041. * don't wanna listen. Ignore it.
  1042. */
  1043. kfree_skb(skb);
  1044. return(0);
  1045. short_packet:
  1046. LIMIT_NETDEBUG(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
  1047. NIPQUAD(saddr),
  1048. ntohs(uh->source),
  1049. ulen,
  1050. len,
  1051. NIPQUAD(daddr),
  1052. ntohs(uh->dest));
  1053. no_header:
  1054. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1055. kfree_skb(skb);
  1056. return(0);
  1057. csum_error:
  1058. /*
  1059. * RFC1122: OK. Discards the bad packet silently (as far as
  1060. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1061. */
  1062. LIMIT_NETDEBUG(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
  1063. NIPQUAD(saddr),
  1064. ntohs(uh->source),
  1065. NIPQUAD(daddr),
  1066. ntohs(uh->dest),
  1067. ulen);
  1068. drop:
  1069. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1070. kfree_skb(skb);
  1071. return(0);
  1072. }
  1073. static int udp_destroy_sock(struct sock *sk)
  1074. {
  1075. lock_sock(sk);
  1076. udp_flush_pending_frames(sk);
  1077. release_sock(sk);
  1078. return 0;
  1079. }
  1080. /*
  1081. * Socket option code for UDP
  1082. */
  1083. static int udp_setsockopt(struct sock *sk, int level, int optname,
  1084. char __user *optval, int optlen)
  1085. {
  1086. struct udp_sock *up = udp_sk(sk);
  1087. int val;
  1088. int err = 0;
  1089. if (level != SOL_UDP)
  1090. return ip_setsockopt(sk, level, optname, optval, optlen);
  1091. if(optlen<sizeof(int))
  1092. return -EINVAL;
  1093. if (get_user(val, (int __user *)optval))
  1094. return -EFAULT;
  1095. switch(optname) {
  1096. case UDP_CORK:
  1097. if (val != 0) {
  1098. up->corkflag = 1;
  1099. } else {
  1100. up->corkflag = 0;
  1101. lock_sock(sk);
  1102. udp_push_pending_frames(sk, up);
  1103. release_sock(sk);
  1104. }
  1105. break;
  1106. case UDP_ENCAP:
  1107. switch (val) {
  1108. case 0:
  1109. case UDP_ENCAP_ESPINUDP:
  1110. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1111. up->encap_type = val;
  1112. break;
  1113. default:
  1114. err = -ENOPROTOOPT;
  1115. break;
  1116. }
  1117. break;
  1118. default:
  1119. err = -ENOPROTOOPT;
  1120. break;
  1121. };
  1122. return err;
  1123. }
  1124. static int 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 (level != SOL_UDP)
  1130. return ip_getsockopt(sk, level, optname, optval, optlen);
  1131. if(get_user(len,optlen))
  1132. return -EFAULT;
  1133. len = min_t(unsigned int, len, sizeof(int));
  1134. if(len < 0)
  1135. return -EINVAL;
  1136. switch(optname) {
  1137. case UDP_CORK:
  1138. val = up->corkflag;
  1139. break;
  1140. case UDP_ENCAP:
  1141. val = up->encap_type;
  1142. break;
  1143. default:
  1144. return -ENOPROTOOPT;
  1145. };
  1146. if(put_user(len, optlen))
  1147. return -EFAULT;
  1148. if(copy_to_user(optval, &val,len))
  1149. return -EFAULT;
  1150. return 0;
  1151. }
  1152. /**
  1153. * udp_poll - wait for a UDP event.
  1154. * @file - file struct
  1155. * @sock - socket
  1156. * @wait - poll table
  1157. *
  1158. * This is same as datagram poll, except for the special case of
  1159. * blocking sockets. If application is using a blocking fd
  1160. * and a packet with checksum error is in the queue;
  1161. * then it could get return from select indicating data available
  1162. * but then block when reading it. Add special case code
  1163. * to work around these arguably broken applications.
  1164. */
  1165. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1166. {
  1167. unsigned int mask = datagram_poll(file, sock, wait);
  1168. struct sock *sk = sock->sk;
  1169. /* Check for false positives due to checksum errors */
  1170. if ( (mask & POLLRDNORM) &&
  1171. !(file->f_flags & O_NONBLOCK) &&
  1172. !(sk->sk_shutdown & RCV_SHUTDOWN)){
  1173. struct sk_buff_head *rcvq = &sk->sk_receive_queue;
  1174. struct sk_buff *skb;
  1175. spin_lock_bh(&rcvq->lock);
  1176. while ((skb = skb_peek(rcvq)) != NULL) {
  1177. if (udp_checksum_complete(skb)) {
  1178. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1179. __skb_unlink(skb, rcvq);
  1180. kfree_skb(skb);
  1181. } else {
  1182. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1183. break;
  1184. }
  1185. }
  1186. spin_unlock_bh(&rcvq->lock);
  1187. /* nothing to see, move along */
  1188. if (skb == NULL)
  1189. mask &= ~(POLLIN | POLLRDNORM);
  1190. }
  1191. return mask;
  1192. }
  1193. struct proto udp_prot = {
  1194. .name = "UDP",
  1195. .owner = THIS_MODULE,
  1196. .close = udp_close,
  1197. .connect = ip4_datagram_connect,
  1198. .disconnect = udp_disconnect,
  1199. .ioctl = udp_ioctl,
  1200. .destroy = udp_destroy_sock,
  1201. .setsockopt = udp_setsockopt,
  1202. .getsockopt = udp_getsockopt,
  1203. .sendmsg = udp_sendmsg,
  1204. .recvmsg = udp_recvmsg,
  1205. .sendpage = udp_sendpage,
  1206. .backlog_rcv = udp_queue_rcv_skb,
  1207. .hash = udp_v4_hash,
  1208. .unhash = udp_v4_unhash,
  1209. .get_port = udp_v4_get_port,
  1210. .obj_size = sizeof(struct udp_sock),
  1211. };
  1212. /* ------------------------------------------------------------------------ */
  1213. #ifdef CONFIG_PROC_FS
  1214. static struct sock *udp_get_first(struct seq_file *seq)
  1215. {
  1216. struct sock *sk;
  1217. struct udp_iter_state *state = seq->private;
  1218. for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
  1219. struct hlist_node *node;
  1220. sk_for_each(sk, node, &udp_hash[state->bucket]) {
  1221. if (sk->sk_family == state->family)
  1222. goto found;
  1223. }
  1224. }
  1225. sk = NULL;
  1226. found:
  1227. return sk;
  1228. }
  1229. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1230. {
  1231. struct udp_iter_state *state = seq->private;
  1232. do {
  1233. sk = sk_next(sk);
  1234. try_again:
  1235. ;
  1236. } while (sk && sk->sk_family != state->family);
  1237. if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
  1238. sk = sk_head(&udp_hash[state->bucket]);
  1239. goto try_again;
  1240. }
  1241. return sk;
  1242. }
  1243. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1244. {
  1245. struct sock *sk = udp_get_first(seq);
  1246. if (sk)
  1247. while(pos && (sk = udp_get_next(seq, sk)) != NULL)
  1248. --pos;
  1249. return pos ? NULL : sk;
  1250. }
  1251. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1252. {
  1253. read_lock(&udp_hash_lock);
  1254. return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
  1255. }
  1256. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1257. {
  1258. struct sock *sk;
  1259. if (v == (void *)1)
  1260. sk = udp_get_idx(seq, 0);
  1261. else
  1262. sk = udp_get_next(seq, v);
  1263. ++*pos;
  1264. return sk;
  1265. }
  1266. static void udp_seq_stop(struct seq_file *seq, void *v)
  1267. {
  1268. read_unlock(&udp_hash_lock);
  1269. }
  1270. static int udp_seq_open(struct inode *inode, struct file *file)
  1271. {
  1272. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1273. struct seq_file *seq;
  1274. int rc = -ENOMEM;
  1275. struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  1276. if (!s)
  1277. goto out;
  1278. memset(s, 0, sizeof(*s));
  1279. s->family = afinfo->family;
  1280. s->seq_ops.start = udp_seq_start;
  1281. s->seq_ops.next = udp_seq_next;
  1282. s->seq_ops.show = afinfo->seq_show;
  1283. s->seq_ops.stop = udp_seq_stop;
  1284. rc = seq_open(file, &s->seq_ops);
  1285. if (rc)
  1286. goto out_kfree;
  1287. seq = file->private_data;
  1288. seq->private = s;
  1289. out:
  1290. return rc;
  1291. out_kfree:
  1292. kfree(s);
  1293. goto out;
  1294. }
  1295. /* ------------------------------------------------------------------------ */
  1296. int udp_proc_register(struct udp_seq_afinfo *afinfo)
  1297. {
  1298. struct proc_dir_entry *p;
  1299. int rc = 0;
  1300. if (!afinfo)
  1301. return -EINVAL;
  1302. afinfo->seq_fops->owner = afinfo->owner;
  1303. afinfo->seq_fops->open = udp_seq_open;
  1304. afinfo->seq_fops->read = seq_read;
  1305. afinfo->seq_fops->llseek = seq_lseek;
  1306. afinfo->seq_fops->release = seq_release_private;
  1307. p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
  1308. if (p)
  1309. p->data = afinfo;
  1310. else
  1311. rc = -ENOMEM;
  1312. return rc;
  1313. }
  1314. void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
  1315. {
  1316. if (!afinfo)
  1317. return;
  1318. proc_net_remove(afinfo->name);
  1319. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1320. }
  1321. /* ------------------------------------------------------------------------ */
  1322. static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
  1323. {
  1324. struct inet_sock *inet = inet_sk(sp);
  1325. unsigned int dest = inet->daddr;
  1326. unsigned int src = inet->rcv_saddr;
  1327. __u16 destp = ntohs(inet->dport);
  1328. __u16 srcp = ntohs(inet->sport);
  1329. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1330. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
  1331. bucket, src, srcp, dest, destp, sp->sk_state,
  1332. atomic_read(&sp->sk_wmem_alloc),
  1333. atomic_read(&sp->sk_rmem_alloc),
  1334. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1335. atomic_read(&sp->sk_refcnt), sp);
  1336. }
  1337. static int udp4_seq_show(struct seq_file *seq, void *v)
  1338. {
  1339. if (v == SEQ_START_TOKEN)
  1340. seq_printf(seq, "%-127s\n",
  1341. " sl local_address rem_address st tx_queue "
  1342. "rx_queue tr tm->when retrnsmt uid timeout "
  1343. "inode");
  1344. else {
  1345. char tmpbuf[129];
  1346. struct udp_iter_state *state = seq->private;
  1347. udp4_format_sock(v, tmpbuf, state->bucket);
  1348. seq_printf(seq, "%-127s\n", tmpbuf);
  1349. }
  1350. return 0;
  1351. }
  1352. /* ------------------------------------------------------------------------ */
  1353. static struct file_operations udp4_seq_fops;
  1354. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1355. .owner = THIS_MODULE,
  1356. .name = "udp",
  1357. .family = AF_INET,
  1358. .seq_show = udp4_seq_show,
  1359. .seq_fops = &udp4_seq_fops,
  1360. };
  1361. int __init udp4_proc_init(void)
  1362. {
  1363. return udp_proc_register(&udp4_seq_afinfo);
  1364. }
  1365. void udp4_proc_exit(void)
  1366. {
  1367. udp_proc_unregister(&udp4_seq_afinfo);
  1368. }
  1369. #endif /* CONFIG_PROC_FS */
  1370. EXPORT_SYMBOL(udp_disconnect);
  1371. EXPORT_SYMBOL(udp_hash);
  1372. EXPORT_SYMBOL(udp_hash_lock);
  1373. EXPORT_SYMBOL(udp_ioctl);
  1374. EXPORT_SYMBOL(udp_port_rover);
  1375. EXPORT_SYMBOL(udp_prot);
  1376. EXPORT_SYMBOL(udp_sendmsg);
  1377. EXPORT_SYMBOL(udp_poll);
  1378. #ifdef CONFIG_PROC_FS
  1379. EXPORT_SYMBOL(udp_proc_register);
  1380. EXPORT_SYMBOL(udp_proc_unregister);
  1381. #endif