udp.c 44 KB

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