udp.c 48 KB

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