udp.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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. if (err == -ENETUNREACH)
  577. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  578. goto out;
  579. }
  580. err = -EACCES;
  581. if ((rt->rt_flags & RTCF_BROADCAST) &&
  582. !sock_flag(sk, SOCK_BROADCAST))
  583. goto out;
  584. if (connected)
  585. sk_dst_set(sk, dst_clone(&rt->u.dst));
  586. }
  587. if (msg->msg_flags&MSG_CONFIRM)
  588. goto do_confirm;
  589. back_from_confirm:
  590. saddr = rt->rt_src;
  591. if (!ipc.addr)
  592. daddr = ipc.addr = rt->rt_dst;
  593. lock_sock(sk);
  594. if (unlikely(up->pending)) {
  595. /* The socket is already corked while preparing it. */
  596. /* ... which is an evident application bug. --ANK */
  597. release_sock(sk);
  598. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  599. err = -EINVAL;
  600. goto out;
  601. }
  602. /*
  603. * Now cork the socket to pend data.
  604. */
  605. inet->cork.fl.fl4_dst = daddr;
  606. inet->cork.fl.fl_ip_dport = dport;
  607. inet->cork.fl.fl4_src = saddr;
  608. inet->cork.fl.fl_ip_sport = inet->sport;
  609. up->pending = AF_INET;
  610. do_append_data:
  611. up->len += ulen;
  612. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  613. err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
  614. sizeof(struct udphdr), &ipc, rt,
  615. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  616. if (err)
  617. udp_flush_pending_frames(sk);
  618. else if (!corkreq)
  619. err = udp_push_pending_frames(sk);
  620. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  621. up->pending = 0;
  622. release_sock(sk);
  623. out:
  624. ip_rt_put(rt);
  625. if (free)
  626. kfree(ipc.opt);
  627. if (!err) {
  628. UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
  629. return len;
  630. }
  631. /*
  632. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  633. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  634. * we don't have a good statistic (IpOutDiscards but it can be too many
  635. * things). We could add another new stat but at least for now that
  636. * seems like overkill.
  637. */
  638. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  639. UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite);
  640. }
  641. return err;
  642. do_confirm:
  643. dst_confirm(&rt->u.dst);
  644. if (!(msg->msg_flags&MSG_PROBE) || len)
  645. goto back_from_confirm;
  646. err = 0;
  647. goto out;
  648. }
  649. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  650. size_t size, int flags)
  651. {
  652. struct udp_sock *up = udp_sk(sk);
  653. int ret;
  654. if (!up->pending) {
  655. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  656. /* Call udp_sendmsg to specify destination address which
  657. * sendpage interface can't pass.
  658. * This will succeed only when the socket is connected.
  659. */
  660. ret = udp_sendmsg(NULL, sk, &msg, 0);
  661. if (ret < 0)
  662. return ret;
  663. }
  664. lock_sock(sk);
  665. if (unlikely(!up->pending)) {
  666. release_sock(sk);
  667. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
  668. return -EINVAL;
  669. }
  670. ret = ip_append_page(sk, page, offset, size, flags);
  671. if (ret == -EOPNOTSUPP) {
  672. release_sock(sk);
  673. return sock_no_sendpage(sk->sk_socket, page, offset,
  674. size, flags);
  675. }
  676. if (ret < 0) {
  677. udp_flush_pending_frames(sk);
  678. goto out;
  679. }
  680. up->len += size;
  681. if (!(up->corkflag || (flags&MSG_MORE)))
  682. ret = udp_push_pending_frames(sk);
  683. if (!ret)
  684. ret = size;
  685. out:
  686. release_sock(sk);
  687. return ret;
  688. }
  689. /*
  690. * IOCTL requests applicable to the UDP protocol
  691. */
  692. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  693. {
  694. switch (cmd) {
  695. case SIOCOUTQ:
  696. {
  697. int amount = atomic_read(&sk->sk_wmem_alloc);
  698. return put_user(amount, (int __user *)arg);
  699. }
  700. case SIOCINQ:
  701. {
  702. struct sk_buff *skb;
  703. unsigned long amount;
  704. amount = 0;
  705. spin_lock_bh(&sk->sk_receive_queue.lock);
  706. skb = skb_peek(&sk->sk_receive_queue);
  707. if (skb != NULL) {
  708. /*
  709. * We will only return the amount
  710. * of this packet since that is all
  711. * that will be read.
  712. */
  713. amount = skb->len - sizeof(struct udphdr);
  714. }
  715. spin_unlock_bh(&sk->sk_receive_queue.lock);
  716. return put_user(amount, (int __user *)arg);
  717. }
  718. default:
  719. return -ENOIOCTLCMD;
  720. }
  721. return 0;
  722. }
  723. /*
  724. * This should be easy, if there is something there we
  725. * return it, otherwise we block.
  726. */
  727. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  728. size_t len, int noblock, int flags, int *addr_len)
  729. {
  730. struct inet_sock *inet = inet_sk(sk);
  731. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  732. struct sk_buff *skb;
  733. unsigned int ulen, copied;
  734. int err;
  735. int is_udplite = IS_UDPLITE(sk);
  736. /*
  737. * Check any passed addresses
  738. */
  739. if (addr_len)
  740. *addr_len=sizeof(*sin);
  741. if (flags & MSG_ERRQUEUE)
  742. return ip_recv_error(sk, msg, len);
  743. try_again:
  744. skb = skb_recv_datagram(sk, flags, noblock, &err);
  745. if (!skb)
  746. goto out;
  747. ulen = skb->len - sizeof(struct udphdr);
  748. copied = len;
  749. if (copied > ulen)
  750. copied = ulen;
  751. else if (copied < ulen)
  752. msg->msg_flags |= MSG_TRUNC;
  753. /*
  754. * If checksum is needed at all, try to do it while copying the
  755. * data. If the data is truncated, or if we only want a partial
  756. * coverage checksum (UDP-Lite), do it before the copy.
  757. */
  758. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  759. if (udp_lib_checksum_complete(skb))
  760. goto csum_copy_err;
  761. }
  762. if (skb_csum_unnecessary(skb))
  763. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  764. msg->msg_iov, copied );
  765. else {
  766. err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
  767. if (err == -EINVAL)
  768. goto csum_copy_err;
  769. }
  770. if (err)
  771. goto out_free;
  772. sock_recv_timestamp(msg, sk, skb);
  773. /* Copy the address. */
  774. if (sin)
  775. {
  776. sin->sin_family = AF_INET;
  777. sin->sin_port = udp_hdr(skb)->source;
  778. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  779. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  780. }
  781. if (inet->cmsg_flags)
  782. ip_cmsg_recv(msg, skb);
  783. err = copied;
  784. if (flags & MSG_TRUNC)
  785. err = ulen;
  786. out_free:
  787. skb_free_datagram(sk, skb);
  788. out:
  789. return err;
  790. csum_copy_err:
  791. UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
  792. skb_kill_datagram(sk, skb, flags);
  793. if (noblock)
  794. return -EAGAIN;
  795. goto try_again;
  796. }
  797. int udp_disconnect(struct sock *sk, int flags)
  798. {
  799. struct inet_sock *inet = inet_sk(sk);
  800. /*
  801. * 1003.1g - break association.
  802. */
  803. sk->sk_state = TCP_CLOSE;
  804. inet->daddr = 0;
  805. inet->dport = 0;
  806. sk->sk_bound_dev_if = 0;
  807. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  808. inet_reset_saddr(sk);
  809. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  810. sk->sk_prot->unhash(sk);
  811. inet->sport = 0;
  812. }
  813. sk_dst_reset(sk);
  814. return 0;
  815. }
  816. /* return:
  817. * 1 if the UDP system should process it
  818. * 0 if we should drop this packet
  819. * -1 if it should get processed by xfrm4_rcv_encap
  820. */
  821. static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
  822. {
  823. #ifndef CONFIG_XFRM
  824. return 1;
  825. #else
  826. struct udp_sock *up = udp_sk(sk);
  827. struct udphdr *uh;
  828. struct iphdr *iph;
  829. int iphlen, len;
  830. __u8 *udpdata;
  831. __be32 *udpdata32;
  832. __u16 encap_type = up->encap_type;
  833. /* if we're overly short, let UDP handle it */
  834. len = skb->len - sizeof(struct udphdr);
  835. if (len <= 0)
  836. return 1;
  837. /* if this is not encapsulated socket, then just return now */
  838. if (!encap_type)
  839. return 1;
  840. /* If this is a paged skb, make sure we pull up
  841. * whatever data we need to look at. */
  842. if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
  843. return 1;
  844. /* Now we can get the pointers */
  845. uh = udp_hdr(skb);
  846. udpdata = (__u8 *)uh + sizeof(struct udphdr);
  847. udpdata32 = (__be32 *)udpdata;
  848. switch (encap_type) {
  849. default:
  850. case UDP_ENCAP_ESPINUDP:
  851. /* Check if this is a keepalive packet. If so, eat it. */
  852. if (len == 1 && udpdata[0] == 0xff) {
  853. return 0;
  854. } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
  855. /* ESP Packet without Non-ESP header */
  856. len = sizeof(struct udphdr);
  857. } else
  858. /* Must be an IKE packet.. pass it through */
  859. return 1;
  860. break;
  861. case UDP_ENCAP_ESPINUDP_NON_IKE:
  862. /* Check if this is a keepalive packet. If so, eat it. */
  863. if (len == 1 && udpdata[0] == 0xff) {
  864. return 0;
  865. } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
  866. udpdata32[0] == 0 && udpdata32[1] == 0) {
  867. /* ESP Packet with Non-IKE marker */
  868. len = sizeof(struct udphdr) + 2 * sizeof(u32);
  869. } else
  870. /* Must be an IKE packet.. pass it through */
  871. return 1;
  872. break;
  873. }
  874. /* At this point we are sure that this is an ESPinUDP packet,
  875. * so we need to remove 'len' bytes from the packet (the UDP
  876. * header and optional ESP marker bytes) and then modify the
  877. * protocol to ESP, and then call into the transform receiver.
  878. */
  879. if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  880. return 0;
  881. /* Now we can update and verify the packet length... */
  882. iph = ip_hdr(skb);
  883. iphlen = iph->ihl << 2;
  884. iph->tot_len = htons(ntohs(iph->tot_len) - len);
  885. if (skb->len < iphlen + len) {
  886. /* packet is too small!?! */
  887. return 0;
  888. }
  889. /* pull the data buffer up to the ESP header and set the
  890. * transport header to point to ESP. Keep UDP on the stack
  891. * for later.
  892. */
  893. __skb_pull(skb, len);
  894. skb_reset_transport_header(skb);
  895. /* modify the protocol (it's ESP!) */
  896. iph->protocol = IPPROTO_ESP;
  897. /* and let the caller know to send this into the ESP processor... */
  898. return -1;
  899. #endif
  900. }
  901. /* returns:
  902. * -1: error
  903. * 0: success
  904. * >0: "udp encap" protocol resubmission
  905. *
  906. * Note that in the success and error cases, the skb is assumed to
  907. * have either been requeued or freed.
  908. */
  909. int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
  910. {
  911. struct udp_sock *up = udp_sk(sk);
  912. int rc;
  913. /*
  914. * Charge it to the socket, dropping if the queue is full.
  915. */
  916. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  917. goto drop;
  918. nf_reset(skb);
  919. if (up->encap_type) {
  920. /*
  921. * This is an encapsulation socket, so let's see if this is
  922. * an encapsulated packet.
  923. * If it's a keepalive packet, then just eat it.
  924. * If it's an encapsulateed packet, then pass it to the
  925. * IPsec xfrm input and return the response
  926. * appropriately. Otherwise, just fall through and
  927. * pass this up the UDP socket.
  928. */
  929. int ret;
  930. ret = udp_encap_rcv(sk, skb);
  931. if (ret == 0) {
  932. /* Eat the packet .. */
  933. kfree_skb(skb);
  934. return 0;
  935. }
  936. if (ret < 0) {
  937. /* process the ESP packet */
  938. ret = xfrm4_rcv_encap(skb, up->encap_type);
  939. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, up->pcflag);
  940. return -ret;
  941. }
  942. /* FALLTHROUGH -- it's a UDP Packet */
  943. }
  944. /*
  945. * UDP-Lite specific tests, ignored on UDP sockets
  946. */
  947. if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  948. /*
  949. * MIB statistics other than incrementing the error count are
  950. * disabled for the following two types of errors: these depend
  951. * on the application settings, not on the functioning of the
  952. * protocol stack as such.
  953. *
  954. * RFC 3828 here recommends (sec 3.3): "There should also be a
  955. * way ... to ... at least let the receiving application block
  956. * delivery of packets with coverage values less than a value
  957. * provided by the application."
  958. */
  959. if (up->pcrlen == 0) { /* full coverage was set */
  960. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  961. "%d while full coverage %d requested\n",
  962. UDP_SKB_CB(skb)->cscov, skb->len);
  963. goto drop;
  964. }
  965. /* The next case involves violating the min. coverage requested
  966. * by the receiver. This is subtle: if receiver wants x and x is
  967. * greater than the buffersize/MTU then receiver will complain
  968. * that it wants x while sender emits packets of smaller size y.
  969. * Therefore the above ...()->partial_cov statement is essential.
  970. */
  971. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  972. LIMIT_NETDEBUG(KERN_WARNING
  973. "UDPLITE: coverage %d too small, need min %d\n",
  974. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  975. goto drop;
  976. }
  977. }
  978. if (sk->sk_filter) {
  979. if (udp_lib_checksum_complete(skb))
  980. goto drop;
  981. }
  982. if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
  983. /* Note that an ENOMEM error is charged twice */
  984. if (rc == -ENOMEM)
  985. UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, up->pcflag);
  986. goto drop;
  987. }
  988. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, up->pcflag);
  989. return 0;
  990. drop:
  991. UDP_INC_STATS_BH(UDP_MIB_INERRORS, up->pcflag);
  992. kfree_skb(skb);
  993. return -1;
  994. }
  995. /*
  996. * Multicasts and broadcasts go to each listener.
  997. *
  998. * Note: called only from the BH handler context,
  999. * so we don't need to lock the hashes.
  1000. */
  1001. static int __udp4_lib_mcast_deliver(struct sk_buff *skb,
  1002. struct udphdr *uh,
  1003. __be32 saddr, __be32 daddr,
  1004. struct hlist_head udptable[])
  1005. {
  1006. struct sock *sk;
  1007. int dif;
  1008. read_lock(&udp_hash_lock);
  1009. sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
  1010. dif = skb->dev->ifindex;
  1011. sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
  1012. if (sk) {
  1013. struct sock *sknext = NULL;
  1014. do {
  1015. struct sk_buff *skb1 = skb;
  1016. sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
  1017. uh->source, saddr, dif);
  1018. if (sknext)
  1019. skb1 = skb_clone(skb, GFP_ATOMIC);
  1020. if (skb1) {
  1021. int ret = udp_queue_rcv_skb(sk, skb1);
  1022. if (ret > 0)
  1023. /* we should probably re-process instead
  1024. * of dropping packets here. */
  1025. kfree_skb(skb1);
  1026. }
  1027. sk = sknext;
  1028. } while (sknext);
  1029. } else
  1030. kfree_skb(skb);
  1031. read_unlock(&udp_hash_lock);
  1032. return 0;
  1033. }
  1034. /* Initialize UDP checksum. If exited with zero value (success),
  1035. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  1036. * Otherwise, csum completion requires chacksumming packet body,
  1037. * including udp header and folding it to skb->csum.
  1038. */
  1039. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  1040. int proto)
  1041. {
  1042. const struct iphdr *iph;
  1043. int err;
  1044. UDP_SKB_CB(skb)->partial_cov = 0;
  1045. UDP_SKB_CB(skb)->cscov = skb->len;
  1046. if (proto == IPPROTO_UDPLITE) {
  1047. err = udplite_checksum_init(skb, uh);
  1048. if (err)
  1049. return err;
  1050. }
  1051. iph = ip_hdr(skb);
  1052. if (uh->check == 0) {
  1053. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1054. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1055. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1056. proto, skb->csum))
  1057. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1058. }
  1059. if (!skb_csum_unnecessary(skb))
  1060. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  1061. skb->len, proto, 0);
  1062. /* Probably, we should checksum udp header (it should be in cache
  1063. * in any case) and data in tiny packets (< rx copybreak).
  1064. */
  1065. return 0;
  1066. }
  1067. /*
  1068. * All we need to do is get the socket, and then do a checksum.
  1069. */
  1070. int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
  1071. int proto)
  1072. {
  1073. struct sock *sk;
  1074. struct udphdr *uh = udp_hdr(skb);
  1075. unsigned short ulen;
  1076. struct rtable *rt = (struct rtable*)skb->dst;
  1077. __be32 saddr = ip_hdr(skb)->saddr;
  1078. __be32 daddr = ip_hdr(skb)->daddr;
  1079. /*
  1080. * Validate the packet.
  1081. */
  1082. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1083. goto drop; /* No space for header. */
  1084. ulen = ntohs(uh->len);
  1085. if (ulen > skb->len)
  1086. goto short_packet;
  1087. if (proto == IPPROTO_UDP) {
  1088. /* UDP validates ulen. */
  1089. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1090. goto short_packet;
  1091. uh = udp_hdr(skb);
  1092. }
  1093. if (udp4_csum_init(skb, uh, proto))
  1094. goto csum_error;
  1095. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1096. return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable);
  1097. sk = __udp4_lib_lookup(saddr, uh->source, daddr, uh->dest,
  1098. skb->dev->ifindex, udptable );
  1099. if (sk != NULL) {
  1100. int ret = udp_queue_rcv_skb(sk, skb);
  1101. sock_put(sk);
  1102. /* a return value > 0 means to resubmit the input, but
  1103. * it wants the return to be -protocol, or 0
  1104. */
  1105. if (ret > 0)
  1106. return -ret;
  1107. return 0;
  1108. }
  1109. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1110. goto drop;
  1111. nf_reset(skb);
  1112. /* No socket. Drop packet silently, if checksum is wrong */
  1113. if (udp_lib_checksum_complete(skb))
  1114. goto csum_error;
  1115. UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1116. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1117. /*
  1118. * Hmm. We got an UDP packet to a port to which we
  1119. * don't wanna listen. Ignore it.
  1120. */
  1121. kfree_skb(skb);
  1122. return 0;
  1123. short_packet:
  1124. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
  1125. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1126. NIPQUAD(saddr),
  1127. ntohs(uh->source),
  1128. ulen,
  1129. skb->len,
  1130. NIPQUAD(daddr),
  1131. ntohs(uh->dest));
  1132. goto drop;
  1133. csum_error:
  1134. /*
  1135. * RFC1122: OK. Discards the bad packet silently (as far as
  1136. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1137. */
  1138. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
  1139. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1140. NIPQUAD(saddr),
  1141. ntohs(uh->source),
  1142. NIPQUAD(daddr),
  1143. ntohs(uh->dest),
  1144. ulen);
  1145. drop:
  1146. UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1147. kfree_skb(skb);
  1148. return 0;
  1149. }
  1150. int udp_rcv(struct sk_buff *skb)
  1151. {
  1152. return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP);
  1153. }
  1154. int udp_destroy_sock(struct sock *sk)
  1155. {
  1156. lock_sock(sk);
  1157. udp_flush_pending_frames(sk);
  1158. release_sock(sk);
  1159. return 0;
  1160. }
  1161. /*
  1162. * Socket option code for UDP
  1163. */
  1164. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1165. char __user *optval, int optlen,
  1166. int (*push_pending_frames)(struct sock *))
  1167. {
  1168. struct udp_sock *up = udp_sk(sk);
  1169. int val;
  1170. int err = 0;
  1171. if (optlen<sizeof(int))
  1172. return -EINVAL;
  1173. if (get_user(val, (int __user *)optval))
  1174. return -EFAULT;
  1175. switch (optname) {
  1176. case UDP_CORK:
  1177. if (val != 0) {
  1178. up->corkflag = 1;
  1179. } else {
  1180. up->corkflag = 0;
  1181. lock_sock(sk);
  1182. (*push_pending_frames)(sk);
  1183. release_sock(sk);
  1184. }
  1185. break;
  1186. case UDP_ENCAP:
  1187. switch (val) {
  1188. case 0:
  1189. case UDP_ENCAP_ESPINUDP:
  1190. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1191. up->encap_type = val;
  1192. break;
  1193. default:
  1194. err = -ENOPROTOOPT;
  1195. break;
  1196. }
  1197. break;
  1198. /*
  1199. * UDP-Lite's partial checksum coverage (RFC 3828).
  1200. */
  1201. /* The sender sets actual checksum coverage length via this option.
  1202. * The case coverage > packet length is handled by send module. */
  1203. case UDPLITE_SEND_CSCOV:
  1204. if (!up->pcflag) /* Disable the option on UDP sockets */
  1205. return -ENOPROTOOPT;
  1206. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1207. val = 8;
  1208. up->pcslen = val;
  1209. up->pcflag |= UDPLITE_SEND_CC;
  1210. break;
  1211. /* The receiver specifies a minimum checksum coverage value. To make
  1212. * sense, this should be set to at least 8 (as done below). If zero is
  1213. * used, this again means full checksum coverage. */
  1214. case UDPLITE_RECV_CSCOV:
  1215. if (!up->pcflag) /* Disable the option on UDP sockets */
  1216. return -ENOPROTOOPT;
  1217. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1218. val = 8;
  1219. up->pcrlen = val;
  1220. up->pcflag |= UDPLITE_RECV_CC;
  1221. break;
  1222. default:
  1223. err = -ENOPROTOOPT;
  1224. break;
  1225. }
  1226. return err;
  1227. }
  1228. int udp_setsockopt(struct sock *sk, int level, int optname,
  1229. char __user *optval, int optlen)
  1230. {
  1231. if (level == SOL_UDP || level == SOL_UDPLITE)
  1232. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1233. udp_push_pending_frames);
  1234. return ip_setsockopt(sk, level, optname, optval, optlen);
  1235. }
  1236. #ifdef CONFIG_COMPAT
  1237. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1238. char __user *optval, int optlen)
  1239. {
  1240. if (level == SOL_UDP || level == SOL_UDPLITE)
  1241. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1242. udp_push_pending_frames);
  1243. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1244. }
  1245. #endif
  1246. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1247. char __user *optval, int __user *optlen)
  1248. {
  1249. struct udp_sock *up = udp_sk(sk);
  1250. int val, len;
  1251. if (get_user(len,optlen))
  1252. return -EFAULT;
  1253. len = min_t(unsigned int, len, sizeof(int));
  1254. if (len < 0)
  1255. return -EINVAL;
  1256. switch (optname) {
  1257. case UDP_CORK:
  1258. val = up->corkflag;
  1259. break;
  1260. case UDP_ENCAP:
  1261. val = up->encap_type;
  1262. break;
  1263. /* The following two cannot be changed on UDP sockets, the return is
  1264. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1265. case UDPLITE_SEND_CSCOV:
  1266. val = up->pcslen;
  1267. break;
  1268. case UDPLITE_RECV_CSCOV:
  1269. val = up->pcrlen;
  1270. break;
  1271. default:
  1272. return -ENOPROTOOPT;
  1273. }
  1274. if (put_user(len, optlen))
  1275. return -EFAULT;
  1276. if (copy_to_user(optval, &val,len))
  1277. return -EFAULT;
  1278. return 0;
  1279. }
  1280. int udp_getsockopt(struct sock *sk, int level, int optname,
  1281. char __user *optval, int __user *optlen)
  1282. {
  1283. if (level == SOL_UDP || level == SOL_UDPLITE)
  1284. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1285. return ip_getsockopt(sk, level, optname, optval, optlen);
  1286. }
  1287. #ifdef CONFIG_COMPAT
  1288. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1289. char __user *optval, int __user *optlen)
  1290. {
  1291. if (level == SOL_UDP || level == SOL_UDPLITE)
  1292. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1293. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1294. }
  1295. #endif
  1296. /**
  1297. * udp_poll - wait for a UDP event.
  1298. * @file - file struct
  1299. * @sock - socket
  1300. * @wait - poll table
  1301. *
  1302. * This is same as datagram poll, except for the special case of
  1303. * blocking sockets. If application is using a blocking fd
  1304. * and a packet with checksum error is in the queue;
  1305. * then it could get return from select indicating data available
  1306. * but then block when reading it. Add special case code
  1307. * to work around these arguably broken applications.
  1308. */
  1309. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1310. {
  1311. unsigned int mask = datagram_poll(file, sock, wait);
  1312. struct sock *sk = sock->sk;
  1313. int is_lite = IS_UDPLITE(sk);
  1314. /* Check for false positives due to checksum errors */
  1315. if ( (mask & POLLRDNORM) &&
  1316. !(file->f_flags & O_NONBLOCK) &&
  1317. !(sk->sk_shutdown & RCV_SHUTDOWN)){
  1318. struct sk_buff_head *rcvq = &sk->sk_receive_queue;
  1319. struct sk_buff *skb;
  1320. spin_lock_bh(&rcvq->lock);
  1321. while ((skb = skb_peek(rcvq)) != NULL &&
  1322. udp_lib_checksum_complete(skb)) {
  1323. UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite);
  1324. __skb_unlink(skb, rcvq);
  1325. kfree_skb(skb);
  1326. }
  1327. spin_unlock_bh(&rcvq->lock);
  1328. /* nothing to see, move along */
  1329. if (skb == NULL)
  1330. mask &= ~(POLLIN | POLLRDNORM);
  1331. }
  1332. return mask;
  1333. }
  1334. struct proto udp_prot = {
  1335. .name = "UDP",
  1336. .owner = THIS_MODULE,
  1337. .close = udp_lib_close,
  1338. .connect = ip4_datagram_connect,
  1339. .disconnect = udp_disconnect,
  1340. .ioctl = udp_ioctl,
  1341. .destroy = udp_destroy_sock,
  1342. .setsockopt = udp_setsockopt,
  1343. .getsockopt = udp_getsockopt,
  1344. .sendmsg = udp_sendmsg,
  1345. .recvmsg = udp_recvmsg,
  1346. .sendpage = udp_sendpage,
  1347. .backlog_rcv = udp_queue_rcv_skb,
  1348. .hash = udp_lib_hash,
  1349. .unhash = udp_lib_unhash,
  1350. .get_port = udp_v4_get_port,
  1351. .obj_size = sizeof(struct udp_sock),
  1352. #ifdef CONFIG_COMPAT
  1353. .compat_setsockopt = compat_udp_setsockopt,
  1354. .compat_getsockopt = compat_udp_getsockopt,
  1355. #endif
  1356. };
  1357. /* ------------------------------------------------------------------------ */
  1358. #ifdef CONFIG_PROC_FS
  1359. static struct sock *udp_get_first(struct seq_file *seq)
  1360. {
  1361. struct sock *sk;
  1362. struct udp_iter_state *state = seq->private;
  1363. for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
  1364. struct hlist_node *node;
  1365. sk_for_each(sk, node, state->hashtable + state->bucket) {
  1366. if (sk->sk_family == state->family)
  1367. goto found;
  1368. }
  1369. }
  1370. sk = NULL;
  1371. found:
  1372. return sk;
  1373. }
  1374. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1375. {
  1376. struct udp_iter_state *state = seq->private;
  1377. do {
  1378. sk = sk_next(sk);
  1379. try_again:
  1380. ;
  1381. } while (sk && sk->sk_family != state->family);
  1382. if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
  1383. sk = sk_head(state->hashtable + state->bucket);
  1384. goto try_again;
  1385. }
  1386. return sk;
  1387. }
  1388. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1389. {
  1390. struct sock *sk = udp_get_first(seq);
  1391. if (sk)
  1392. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  1393. --pos;
  1394. return pos ? NULL : sk;
  1395. }
  1396. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1397. {
  1398. read_lock(&udp_hash_lock);
  1399. return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
  1400. }
  1401. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1402. {
  1403. struct sock *sk;
  1404. if (v == (void *)1)
  1405. sk = udp_get_idx(seq, 0);
  1406. else
  1407. sk = udp_get_next(seq, v);
  1408. ++*pos;
  1409. return sk;
  1410. }
  1411. static void udp_seq_stop(struct seq_file *seq, void *v)
  1412. {
  1413. read_unlock(&udp_hash_lock);
  1414. }
  1415. static int udp_seq_open(struct inode *inode, struct file *file)
  1416. {
  1417. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1418. struct seq_file *seq;
  1419. int rc = -ENOMEM;
  1420. struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  1421. if (!s)
  1422. goto out;
  1423. s->family = afinfo->family;
  1424. s->hashtable = afinfo->hashtable;
  1425. s->seq_ops.start = udp_seq_start;
  1426. s->seq_ops.next = udp_seq_next;
  1427. s->seq_ops.show = afinfo->seq_show;
  1428. s->seq_ops.stop = udp_seq_stop;
  1429. rc = seq_open(file, &s->seq_ops);
  1430. if (rc)
  1431. goto out_kfree;
  1432. seq = file->private_data;
  1433. seq->private = s;
  1434. out:
  1435. return rc;
  1436. out_kfree:
  1437. kfree(s);
  1438. goto out;
  1439. }
  1440. /* ------------------------------------------------------------------------ */
  1441. int udp_proc_register(struct udp_seq_afinfo *afinfo)
  1442. {
  1443. struct proc_dir_entry *p;
  1444. int rc = 0;
  1445. if (!afinfo)
  1446. return -EINVAL;
  1447. afinfo->seq_fops->owner = afinfo->owner;
  1448. afinfo->seq_fops->open = udp_seq_open;
  1449. afinfo->seq_fops->read = seq_read;
  1450. afinfo->seq_fops->llseek = seq_lseek;
  1451. afinfo->seq_fops->release = seq_release_private;
  1452. p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
  1453. if (p)
  1454. p->data = afinfo;
  1455. else
  1456. rc = -ENOMEM;
  1457. return rc;
  1458. }
  1459. void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
  1460. {
  1461. if (!afinfo)
  1462. return;
  1463. proc_net_remove(afinfo->name);
  1464. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1465. }
  1466. /* ------------------------------------------------------------------------ */
  1467. static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
  1468. {
  1469. struct inet_sock *inet = inet_sk(sp);
  1470. __be32 dest = inet->daddr;
  1471. __be32 src = inet->rcv_saddr;
  1472. __u16 destp = ntohs(inet->dport);
  1473. __u16 srcp = ntohs(inet->sport);
  1474. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1475. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
  1476. bucket, src, srcp, dest, destp, sp->sk_state,
  1477. atomic_read(&sp->sk_wmem_alloc),
  1478. atomic_read(&sp->sk_rmem_alloc),
  1479. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1480. atomic_read(&sp->sk_refcnt), sp);
  1481. }
  1482. int udp4_seq_show(struct seq_file *seq, void *v)
  1483. {
  1484. if (v == SEQ_START_TOKEN)
  1485. seq_printf(seq, "%-127s\n",
  1486. " sl local_address rem_address st tx_queue "
  1487. "rx_queue tr tm->when retrnsmt uid timeout "
  1488. "inode");
  1489. else {
  1490. char tmpbuf[129];
  1491. struct udp_iter_state *state = seq->private;
  1492. udp4_format_sock(v, tmpbuf, state->bucket);
  1493. seq_printf(seq, "%-127s\n", tmpbuf);
  1494. }
  1495. return 0;
  1496. }
  1497. /* ------------------------------------------------------------------------ */
  1498. static struct file_operations udp4_seq_fops;
  1499. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1500. .owner = THIS_MODULE,
  1501. .name = "udp",
  1502. .family = AF_INET,
  1503. .hashtable = udp_hash,
  1504. .seq_show = udp4_seq_show,
  1505. .seq_fops = &udp4_seq_fops,
  1506. };
  1507. int __init udp4_proc_init(void)
  1508. {
  1509. return udp_proc_register(&udp4_seq_afinfo);
  1510. }
  1511. void udp4_proc_exit(void)
  1512. {
  1513. udp_proc_unregister(&udp4_seq_afinfo);
  1514. }
  1515. #endif /* CONFIG_PROC_FS */
  1516. EXPORT_SYMBOL(udp_disconnect);
  1517. EXPORT_SYMBOL(udp_hash);
  1518. EXPORT_SYMBOL(udp_hash_lock);
  1519. EXPORT_SYMBOL(udp_ioctl);
  1520. EXPORT_SYMBOL(udp_get_port);
  1521. EXPORT_SYMBOL(udp_prot);
  1522. EXPORT_SYMBOL(udp_sendmsg);
  1523. EXPORT_SYMBOL(udp_lib_getsockopt);
  1524. EXPORT_SYMBOL(udp_lib_setsockopt);
  1525. EXPORT_SYMBOL(udp_poll);
  1526. #ifdef CONFIG_PROC_FS
  1527. EXPORT_SYMBOL(udp_proc_register);
  1528. EXPORT_SYMBOL(udp_proc_unregister);
  1529. #endif