udp.c 45 KB

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