udp.c 43 KB

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