udp.c 39 KB

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