[PATCH 4/8]: Factor out common code for generating Resets

2007-09-25 Thread Gerrit Renker
[DCCP]: Factor out common code for generating Resets

This factors code common to dccp_v{4,6}_ctl_send_reset into a separate function,
and adds support for filling in the Data 1 ... Data 3 fields from RFC 4340, 
5.6. 

It is useful to have this separate, since the following Reset codes will always
be generated from the control socket rather than via dccp_send_reset:
 * Code 3, "No Connection", cf. 8.3.1;
 * Code 4, "Packet Error" (identification for Data 1 added);
 * Code 5, "Option Error" (identification for Data 1..3 added, will be used 
later);
 * Code 6, "Mandatory Error" (same as Option Error);
 * Code 7, "Connection Refused" (what on Earth is the difference to "No 
Connection"?);
 * Code 8, "Bad Service Code";
 * Code 9, "Too Busy";
 * Code 10, "Bad Init Cookie" (not used).

Code 0 is not recommended by the RFC, the following codes would be used in 
dccp_send_reset() instead, since they all relate to an established DCCP 
connection:
 * Code 1, "Closed";
 * Code 2, "Aborted";
 * Code 11, "Aggression Penalty" (12.3).

Signed-off-by: Gerrit Renker <[EMAIL PROTECTED]>
---
 net/dccp/dccp.h   |2 ++
 net/dccp/ipv4.c   |   38 +-
 net/dccp/ipv6.c   |   39 ++-
 net/dccp/output.c |   52 
 4 files changed, 65 insertions(+), 66 deletions(-)

--- a/net/dccp/dccp.h
+++ b/net/dccp/dccp.h
@@ -298,6 +298,8 @@ extern unsigned int dccp_poll(struct fil
 extern intdccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
   int addr_len);
 
+extern struct sk_buff *
+  dccp_ctl_make_reset(struct socket *ctl, struct sk_buff *skb);
 extern intdccp_send_reset(struct sock *sk, enum dccp_reset_codes code);
 extern void   dccp_send_close(struct sock *sk, const int active);
 extern intdccp_invalid_packet(struct sk_buff *skb);
--- a/net/dccp/output.c
+++ b/net/dccp/output.c
@@ -331,6 +331,58 @@ struct sk_buff *dccp_make_response(struc
 
 EXPORT_SYMBOL_GPL(dccp_make_response);
 
+/* answer offending packet in @rcv_skb with Reset from control socket @ctl */
+struct sk_buff *dccp_ctl_make_reset(struct socket *ctl, struct sk_buff 
*rcv_skb)
+{
+   struct dccp_hdr *rxdh = dccp_hdr(rcv_skb), *dh;
+   struct dccp_skb_cb *dcb = DCCP_SKB_CB(rcv_skb);
+   const u32 dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
+  sizeof(struct dccp_hdr_ext) +
+  sizeof(struct dccp_hdr_reset);
+   struct dccp_hdr_reset *dhr;
+   struct sk_buff *skb;
+
+   skb = alloc_skb(ctl->sk->sk_prot->max_header, GFP_ATOMIC);
+   if (skb == NULL)
+   return NULL;
+
+   skb_reserve(skb, ctl->sk->sk_prot->max_header);
+
+   /* Swap the send and the receive. */
+   dh = dccp_zeroed_hdr(skb, dccp_hdr_reset_len);
+   dh->dccph_type  = DCCP_PKT_RESET;
+   dh->dccph_sport = rxdh->dccph_dport;
+   dh->dccph_dport = rxdh->dccph_sport;
+   dh->dccph_doff  = dccp_hdr_reset_len / 4;
+   dh->dccph_x = 1;
+
+   dhr = dccp_hdr_reset(skb);
+   dhr->dccph_reset_code = dcb->dccpd_reset_code;
+
+   switch (dcb->dccpd_reset_code) {
+   case DCCP_RESET_CODE_PACKET_ERROR:
+   dhr->dccph_reset_data[0] = rxdh->dccph_type;
+   break;
+   case DCCP_RESET_CODE_OPTION_ERROR:  /* fall through */
+   case DCCP_RESET_CODE_MANDATORY_ERROR:
+   memcpy(dhr->dccph_reset_data, dcb->dccpd_reset_data, 3);
+   break;
+   }
+   /*
+* From RFC 4340, 8.3.1:
+*   If P.ackno exists, set R.seqno := P.ackno + 1.
+*   Else set R.seqno := 0.
+*/
+   if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
+   dccp_hdr_set_seq(dh, ADD48(dcb->dccpd_ack_seq, 1));
+   dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dcb->dccpd_seq);
+
+   dccp_csum_outgoing(skb);
+   return skb;
+}
+
+EXPORT_SYMBOL_GPL(dccp_ctl_make_reset);
+
 /* send Reset on established socket, to close or abort the connection */
 int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code)
 {
--- a/net/dccp/ipv4.c
+++ b/net/dccp/ipv4.c
@@ -512,17 +512,12 @@ out:
 static void dccp_v4_ctl_send_reset(struct sock *sk, struct sk_buff *rxskb)
 {
int err;
-   struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
const struct iphdr *rxiph;
-   const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
-  sizeof(struct dccp_hdr_ext) +
-  sizeof(struct dccp_hdr_reset);
struct sk_buff *skb;
struct dst_entry *dst;
-   u64 seqno = 0;
 
/* Never send a reset in response to a reset. */
-   if (rxdh->dccph_type == DCCP_PKT_RESET)
+   if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
return;
 
if (((struct rtable *)rxskb->dst)->rt_type != RTN_

Re: [PATCH 4/8]: Factor out common code for generating Resets

2007-09-25 Thread Ian McDonald
On 9/26/07, Gerrit Renker <[EMAIL PROTECTED]> wrote:
> [DCCP]: Factor out common code for generating Resets
>
> This factors code common to dccp_v{4,6}_ctl_send_reset into a separate 
> function,
> and adds support for filling in the Data 1 ... Data 3 fields from RFC 4340, 
> 5.6.
>
> It is useful to have this separate, since the following Reset codes will 
> always
> be generated from the control socket rather than via dccp_send_reset:
>  * Code 3, "No Connection", cf. 8.3.1;
>  * Code 4, "Packet Error" (identification for Data 1 added);
>  * Code 5, "Option Error" (identification for Data 1..3 added, will be used 
> later);
>  * Code 6, "Mandatory Error" (same as Option Error);
>  * Code 7, "Connection Refused" (what on Earth is the difference to "No 
> Connection"?);
>  * Code 8, "Bad Service Code";
>  * Code 9, "Too Busy";
>  * Code 10, "Bad Init Cookie" (not used).
>
> Code 0 is not recommended by the RFC, the following codes would be used in
> dccp_send_reset() instead, since they all relate to an established DCCP 
> connection:
>  * Code 1, "Closed";
>  * Code 2, "Aborted";
>  * Code 11, "Aggression Penalty" (12.3).
>
> Signed-off-by: Gerrit Renker <[EMAIL PROTECTED]>

Acked-by: Ian McDonald <[EMAIL PROTECTED]>
-- 
Web1: http://wand.net.nz/~iam4/
Web2: http://www.jandi.co.nz
Blog: http://iansblog.jandi.co.nz
-
To unsubscribe from this list: send the line "unsubscribe dccp" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html