udp.c 44 KB

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