ip_options.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 options processing module for ip.c
  7. *
  8. * Version: $Id: ip_options.c,v 1.21 2001/09/01 00:31:50 davem Exp $
  9. *
  10. * Authors: A.N.Kuznetsov
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <asm/uaccess.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/ip.h>
  18. #include <linux/icmp.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/rtnetlink.h>
  21. #include <net/sock.h>
  22. #include <net/ip.h>
  23. #include <net/icmp.h>
  24. /*
  25. * Write options to IP header, record destination address to
  26. * source route option, address of outgoing interface
  27. * (we should already know it, so that this function is allowed be
  28. * called only after routing decision) and timestamp,
  29. * if we originate this datagram.
  30. *
  31. * daddr is real destination address, next hop is recorded in IP header.
  32. * saddr is address of outgoing interface.
  33. */
  34. void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
  35. u32 daddr, struct rtable *rt, int is_frag)
  36. {
  37. unsigned char * iph = skb->nh.raw;
  38. memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
  39. memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen);
  40. opt = &(IPCB(skb)->opt);
  41. opt->is_data = 0;
  42. if (opt->srr)
  43. memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4);
  44. if (!is_frag) {
  45. if (opt->rr_needaddr)
  46. ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, rt);
  47. if (opt->ts_needaddr)
  48. ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, rt);
  49. if (opt->ts_needtime) {
  50. struct timeval tv;
  51. __u32 midtime;
  52. do_gettimeofday(&tv);
  53. midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
  54. memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
  55. }
  56. return;
  57. }
  58. if (opt->rr) {
  59. memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]);
  60. opt->rr = 0;
  61. opt->rr_needaddr = 0;
  62. }
  63. if (opt->ts) {
  64. memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]);
  65. opt->ts = 0;
  66. opt->ts_needaddr = opt->ts_needtime = 0;
  67. }
  68. }
  69. /*
  70. * Provided (sopt, skb) points to received options,
  71. * build in dopt compiled option set appropriate for answering.
  72. * i.e. invert SRR option, copy anothers,
  73. * and grab room in RR/TS options.
  74. *
  75. * NOTE: dopt cannot point to skb.
  76. */
  77. int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
  78. {
  79. struct ip_options *sopt;
  80. unsigned char *sptr, *dptr;
  81. int soffset, doffset;
  82. int optlen;
  83. u32 daddr;
  84. memset(dopt, 0, sizeof(struct ip_options));
  85. dopt->is_data = 1;
  86. sopt = &(IPCB(skb)->opt);
  87. if (sopt->optlen == 0) {
  88. dopt->optlen = 0;
  89. return 0;
  90. }
  91. sptr = skb->nh.raw;
  92. dptr = dopt->__data;
  93. if (skb->dst)
  94. daddr = ((struct rtable*)skb->dst)->rt_spec_dst;
  95. else
  96. daddr = skb->nh.iph->daddr;
  97. if (sopt->rr) {
  98. optlen = sptr[sopt->rr+1];
  99. soffset = sptr[sopt->rr+2];
  100. dopt->rr = dopt->optlen + sizeof(struct iphdr);
  101. memcpy(dptr, sptr+sopt->rr, optlen);
  102. if (sopt->rr_needaddr && soffset <= optlen) {
  103. if (soffset + 3 > optlen)
  104. return -EINVAL;
  105. dptr[2] = soffset + 4;
  106. dopt->rr_needaddr = 1;
  107. }
  108. dptr += optlen;
  109. dopt->optlen += optlen;
  110. }
  111. if (sopt->ts) {
  112. optlen = sptr[sopt->ts+1];
  113. soffset = sptr[sopt->ts+2];
  114. dopt->ts = dopt->optlen + sizeof(struct iphdr);
  115. memcpy(dptr, sptr+sopt->ts, optlen);
  116. if (soffset <= optlen) {
  117. if (sopt->ts_needaddr) {
  118. if (soffset + 3 > optlen)
  119. return -EINVAL;
  120. dopt->ts_needaddr = 1;
  121. soffset += 4;
  122. }
  123. if (sopt->ts_needtime) {
  124. if (soffset + 3 > optlen)
  125. return -EINVAL;
  126. if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
  127. dopt->ts_needtime = 1;
  128. soffset += 4;
  129. } else {
  130. dopt->ts_needtime = 0;
  131. if (soffset + 8 <= optlen) {
  132. __u32 addr;
  133. memcpy(&addr, sptr+soffset-1, 4);
  134. if (inet_addr_type(addr) != RTN_LOCAL) {
  135. dopt->ts_needtime = 1;
  136. soffset += 8;
  137. }
  138. }
  139. }
  140. }
  141. dptr[2] = soffset;
  142. }
  143. dptr += optlen;
  144. dopt->optlen += optlen;
  145. }
  146. if (sopt->srr) {
  147. unsigned char * start = sptr+sopt->srr;
  148. u32 faddr;
  149. optlen = start[1];
  150. soffset = start[2];
  151. doffset = 0;
  152. if (soffset > optlen)
  153. soffset = optlen + 1;
  154. soffset -= 4;
  155. if (soffset > 3) {
  156. memcpy(&faddr, &start[soffset-1], 4);
  157. for (soffset-=4, doffset=4; soffset > 3; soffset-=4, doffset+=4)
  158. memcpy(&dptr[doffset-1], &start[soffset-1], 4);
  159. /*
  160. * RFC1812 requires to fix illegal source routes.
  161. */
  162. if (memcmp(&skb->nh.iph->saddr, &start[soffset+3], 4) == 0)
  163. doffset -= 4;
  164. }
  165. if (doffset > 3) {
  166. memcpy(&start[doffset-1], &daddr, 4);
  167. dopt->faddr = faddr;
  168. dptr[0] = start[0];
  169. dptr[1] = doffset+3;
  170. dptr[2] = 4;
  171. dptr += doffset+3;
  172. dopt->srr = dopt->optlen + sizeof(struct iphdr);
  173. dopt->optlen += doffset+3;
  174. dopt->is_strictroute = sopt->is_strictroute;
  175. }
  176. }
  177. while (dopt->optlen & 3) {
  178. *dptr++ = IPOPT_END;
  179. dopt->optlen++;
  180. }
  181. return 0;
  182. }
  183. /*
  184. * Options "fragmenting", just fill options not
  185. * allowed in fragments with NOOPs.
  186. * Simple and stupid 8), but the most efficient way.
  187. */
  188. void ip_options_fragment(struct sk_buff * skb)
  189. {
  190. unsigned char * optptr = skb->nh.raw;
  191. struct ip_options * opt = &(IPCB(skb)->opt);
  192. int l = opt->optlen;
  193. int optlen;
  194. while (l > 0) {
  195. switch (*optptr) {
  196. case IPOPT_END:
  197. return;
  198. case IPOPT_NOOP:
  199. l--;
  200. optptr++;
  201. continue;
  202. }
  203. optlen = optptr[1];
  204. if (optlen<2 || optlen>l)
  205. return;
  206. if (!IPOPT_COPIED(*optptr))
  207. memset(optptr, IPOPT_NOOP, optlen);
  208. l -= optlen;
  209. optptr += optlen;
  210. }
  211. opt->ts = 0;
  212. opt->rr = 0;
  213. opt->rr_needaddr = 0;
  214. opt->ts_needaddr = 0;
  215. opt->ts_needtime = 0;
  216. return;
  217. }
  218. /*
  219. * Verify options and fill pointers in struct options.
  220. * Caller should clear *opt, and set opt->data.
  221. * If opt == NULL, then skb->data should point to IP header.
  222. */
  223. int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
  224. {
  225. int l;
  226. unsigned char * iph;
  227. unsigned char * optptr;
  228. int optlen;
  229. unsigned char * pp_ptr = NULL;
  230. struct rtable *rt = skb ? (struct rtable*)skb->dst : NULL;
  231. if (!opt) {
  232. opt = &(IPCB(skb)->opt);
  233. memset(opt, 0, sizeof(struct ip_options));
  234. iph = skb->nh.raw;
  235. opt->optlen = ((struct iphdr *)iph)->ihl*4 - sizeof(struct iphdr);
  236. optptr = iph + sizeof(struct iphdr);
  237. opt->is_data = 0;
  238. } else {
  239. optptr = opt->is_data ? opt->__data : (unsigned char*)&(skb->nh.iph[1]);
  240. iph = optptr - sizeof(struct iphdr);
  241. }
  242. for (l = opt->optlen; l > 0; ) {
  243. switch (*optptr) {
  244. case IPOPT_END:
  245. for (optptr++, l--; l>0; optptr++, l--) {
  246. if (*optptr != IPOPT_END) {
  247. *optptr = IPOPT_END;
  248. opt->is_changed = 1;
  249. }
  250. }
  251. goto eol;
  252. case IPOPT_NOOP:
  253. l--;
  254. optptr++;
  255. continue;
  256. }
  257. optlen = optptr[1];
  258. if (optlen<2 || optlen>l) {
  259. pp_ptr = optptr;
  260. goto error;
  261. }
  262. switch (*optptr) {
  263. case IPOPT_SSRR:
  264. case IPOPT_LSRR:
  265. if (optlen < 3) {
  266. pp_ptr = optptr + 1;
  267. goto error;
  268. }
  269. if (optptr[2] < 4) {
  270. pp_ptr = optptr + 2;
  271. goto error;
  272. }
  273. /* NB: cf RFC-1812 5.2.4.1 */
  274. if (opt->srr) {
  275. pp_ptr = optptr;
  276. goto error;
  277. }
  278. if (!skb) {
  279. if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
  280. pp_ptr = optptr + 1;
  281. goto error;
  282. }
  283. memcpy(&opt->faddr, &optptr[3], 4);
  284. if (optlen > 7)
  285. memmove(&optptr[3], &optptr[7], optlen-7);
  286. }
  287. opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
  288. opt->srr = optptr - iph;
  289. break;
  290. case IPOPT_RR:
  291. if (opt->rr) {
  292. pp_ptr = optptr;
  293. goto error;
  294. }
  295. if (optlen < 3) {
  296. pp_ptr = optptr + 1;
  297. goto error;
  298. }
  299. if (optptr[2] < 4) {
  300. pp_ptr = optptr + 2;
  301. goto error;
  302. }
  303. if (optptr[2] <= optlen) {
  304. if (optptr[2]+3 > optlen) {
  305. pp_ptr = optptr + 2;
  306. goto error;
  307. }
  308. if (skb) {
  309. memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
  310. opt->is_changed = 1;
  311. }
  312. optptr[2] += 4;
  313. opt->rr_needaddr = 1;
  314. }
  315. opt->rr = optptr - iph;
  316. break;
  317. case IPOPT_TIMESTAMP:
  318. if (opt->ts) {
  319. pp_ptr = optptr;
  320. goto error;
  321. }
  322. if (optlen < 4) {
  323. pp_ptr = optptr + 1;
  324. goto error;
  325. }
  326. if (optptr[2] < 5) {
  327. pp_ptr = optptr + 2;
  328. goto error;
  329. }
  330. if (optptr[2] <= optlen) {
  331. __u32 * timeptr = NULL;
  332. if (optptr[2]+3 > optptr[1]) {
  333. pp_ptr = optptr + 2;
  334. goto error;
  335. }
  336. switch (optptr[3]&0xF) {
  337. case IPOPT_TS_TSONLY:
  338. opt->ts = optptr - iph;
  339. if (skb)
  340. timeptr = (__u32*)&optptr[optptr[2]-1];
  341. opt->ts_needtime = 1;
  342. optptr[2] += 4;
  343. break;
  344. case IPOPT_TS_TSANDADDR:
  345. if (optptr[2]+7 > optptr[1]) {
  346. pp_ptr = optptr + 2;
  347. goto error;
  348. }
  349. opt->ts = optptr - iph;
  350. if (skb) {
  351. memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
  352. timeptr = (__u32*)&optptr[optptr[2]+3];
  353. }
  354. opt->ts_needaddr = 1;
  355. opt->ts_needtime = 1;
  356. optptr[2] += 8;
  357. break;
  358. case IPOPT_TS_PRESPEC:
  359. if (optptr[2]+7 > optptr[1]) {
  360. pp_ptr = optptr + 2;
  361. goto error;
  362. }
  363. opt->ts = optptr - iph;
  364. {
  365. u32 addr;
  366. memcpy(&addr, &optptr[optptr[2]-1], 4);
  367. if (inet_addr_type(addr) == RTN_UNICAST)
  368. break;
  369. if (skb)
  370. timeptr = (__u32*)&optptr[optptr[2]+3];
  371. }
  372. opt->ts_needtime = 1;
  373. optptr[2] += 8;
  374. break;
  375. default:
  376. if (!skb && !capable(CAP_NET_RAW)) {
  377. pp_ptr = optptr + 3;
  378. goto error;
  379. }
  380. break;
  381. }
  382. if (timeptr) {
  383. struct timeval tv;
  384. __u32 midtime;
  385. do_gettimeofday(&tv);
  386. midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
  387. memcpy(timeptr, &midtime, sizeof(__u32));
  388. opt->is_changed = 1;
  389. }
  390. } else {
  391. unsigned overflow = optptr[3]>>4;
  392. if (overflow == 15) {
  393. pp_ptr = optptr + 3;
  394. goto error;
  395. }
  396. opt->ts = optptr - iph;
  397. if (skb) {
  398. optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
  399. opt->is_changed = 1;
  400. }
  401. }
  402. break;
  403. case IPOPT_RA:
  404. if (optlen < 4) {
  405. pp_ptr = optptr + 1;
  406. goto error;
  407. }
  408. if (optptr[2] == 0 && optptr[3] == 0)
  409. opt->router_alert = optptr - iph;
  410. break;
  411. case IPOPT_SEC:
  412. case IPOPT_SID:
  413. default:
  414. if (!skb && !capable(CAP_NET_RAW)) {
  415. pp_ptr = optptr;
  416. goto error;
  417. }
  418. break;
  419. }
  420. l -= optlen;
  421. optptr += optlen;
  422. }
  423. eol:
  424. if (!pp_ptr)
  425. return 0;
  426. error:
  427. if (skb) {
  428. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24));
  429. }
  430. return -EINVAL;
  431. }
  432. /*
  433. * Undo all the changes done by ip_options_compile().
  434. */
  435. void ip_options_undo(struct ip_options * opt)
  436. {
  437. if (opt->srr) {
  438. unsigned char * optptr = opt->__data+opt->srr-sizeof(struct iphdr);
  439. memmove(optptr+7, optptr+3, optptr[1]-7);
  440. memcpy(optptr+3, &opt->faddr, 4);
  441. }
  442. if (opt->rr_needaddr) {
  443. unsigned char * optptr = opt->__data+opt->rr-sizeof(struct iphdr);
  444. optptr[2] -= 4;
  445. memset(&optptr[optptr[2]-1], 0, 4);
  446. }
  447. if (opt->ts) {
  448. unsigned char * optptr = opt->__data+opt->ts-sizeof(struct iphdr);
  449. if (opt->ts_needtime) {
  450. optptr[2] -= 4;
  451. memset(&optptr[optptr[2]-1], 0, 4);
  452. if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC)
  453. optptr[2] -= 4;
  454. }
  455. if (opt->ts_needaddr) {
  456. optptr[2] -= 4;
  457. memset(&optptr[optptr[2]-1], 0, 4);
  458. }
  459. }
  460. }
  461. int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen, int user)
  462. {
  463. struct ip_options *opt;
  464. opt = kmalloc(sizeof(struct ip_options)+((optlen+3)&~3), GFP_KERNEL);
  465. if (!opt)
  466. return -ENOMEM;
  467. memset(opt, 0, sizeof(struct ip_options));
  468. if (optlen) {
  469. if (user) {
  470. if (copy_from_user(opt->__data, data, optlen)) {
  471. kfree(opt);
  472. return -EFAULT;
  473. }
  474. } else
  475. memcpy(opt->__data, data, optlen);
  476. }
  477. while (optlen & 3)
  478. opt->__data[optlen++] = IPOPT_END;
  479. opt->optlen = optlen;
  480. opt->is_data = 1;
  481. opt->is_setbyuser = 1;
  482. if (optlen && ip_options_compile(opt, NULL)) {
  483. kfree(opt);
  484. return -EINVAL;
  485. }
  486. if (*optp)
  487. kfree(*optp);
  488. *optp = opt;
  489. return 0;
  490. }
  491. void ip_forward_options(struct sk_buff *skb)
  492. {
  493. struct ip_options * opt = &(IPCB(skb)->opt);
  494. unsigned char * optptr;
  495. struct rtable *rt = (struct rtable*)skb->dst;
  496. unsigned char *raw = skb->nh.raw;
  497. if (opt->rr_needaddr) {
  498. optptr = (unsigned char *)raw + opt->rr;
  499. ip_rt_get_source(&optptr[optptr[2]-5], rt);
  500. opt->is_changed = 1;
  501. }
  502. if (opt->srr_is_hit) {
  503. int srrptr, srrspace;
  504. optptr = raw + opt->srr;
  505. for ( srrptr=optptr[2], srrspace = optptr[1];
  506. srrptr <= srrspace;
  507. srrptr += 4
  508. ) {
  509. if (srrptr + 3 > srrspace)
  510. break;
  511. if (memcmp(&rt->rt_dst, &optptr[srrptr-1], 4) == 0)
  512. break;
  513. }
  514. if (srrptr + 3 <= srrspace) {
  515. opt->is_changed = 1;
  516. ip_rt_get_source(&optptr[srrptr-1], rt);
  517. skb->nh.iph->daddr = rt->rt_dst;
  518. optptr[2] = srrptr+4;
  519. } else if (net_ratelimit())
  520. printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n");
  521. if (opt->ts_needaddr) {
  522. optptr = raw + opt->ts;
  523. ip_rt_get_source(&optptr[optptr[2]-9], rt);
  524. opt->is_changed = 1;
  525. }
  526. }
  527. if (opt->is_changed) {
  528. opt->is_changed = 0;
  529. ip_send_check(skb->nh.iph);
  530. }
  531. }
  532. int ip_options_rcv_srr(struct sk_buff *skb)
  533. {
  534. struct ip_options *opt = &(IPCB(skb)->opt);
  535. int srrspace, srrptr;
  536. u32 nexthop;
  537. struct iphdr *iph = skb->nh.iph;
  538. unsigned char * optptr = skb->nh.raw + opt->srr;
  539. struct rtable *rt = (struct rtable*)skb->dst;
  540. struct rtable *rt2;
  541. int err;
  542. if (!opt->srr)
  543. return 0;
  544. if (skb->pkt_type != PACKET_HOST)
  545. return -EINVAL;
  546. if (rt->rt_type == RTN_UNICAST) {
  547. if (!opt->is_strictroute)
  548. return 0;
  549. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
  550. return -EINVAL;
  551. }
  552. if (rt->rt_type != RTN_LOCAL)
  553. return -EINVAL;
  554. for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
  555. if (srrptr + 3 > srrspace) {
  556. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
  557. return -EINVAL;
  558. }
  559. memcpy(&nexthop, &optptr[srrptr-1], 4);
  560. rt = (struct rtable*)skb->dst;
  561. skb->dst = NULL;
  562. err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
  563. rt2 = (struct rtable*)skb->dst;
  564. if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
  565. ip_rt_put(rt2);
  566. skb->dst = &rt->u.dst;
  567. return -EINVAL;
  568. }
  569. ip_rt_put(rt);
  570. if (rt2->rt_type != RTN_LOCAL)
  571. break;
  572. /* Superfast 8) loopback forward */
  573. memcpy(&iph->daddr, &optptr[srrptr-1], 4);
  574. opt->is_changed = 1;
  575. }
  576. if (srrptr <= srrspace) {
  577. opt->srr_is_hit = 1;
  578. opt->is_changed = 1;
  579. }
  580. return 0;
  581. }
  582. EXPORT_SYMBOL(ip_options_compile);
  583. EXPORT_SYMBOL(ip_options_undo);