udp.c 46 KB

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