* Copyright (c) 1991 The Regents of the University of California.
* %sccs.include.redist.c%
* @(#)clnp_frag.c 7.12 (Berkeley) %G%
/***********************************************************
Copyright IBM Corporation 1987
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of IBM not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
******************************************************************/
* ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
/* $Header: /var/src/sys/netiso/RCS/clnp_frag.c,v 5.1 89/02/09 16:20:26 hagens Exp $ */
/* $Source: /var/src/sys/netiso/RCS/clnp_frag.c,v $ */
#include "../net/route.h"
/* all fragments are hung off this list */
struct clnp_fragl
*clnp_frags
= NULL
;
struct mbuf
*clnp_comp_pdu();
* FUNCTION: clnp_fragment
* PURPOSE: Fragment a datagram, and send the itty bitty pieces
* failure - unix error code
* NOTES: If there is an error sending the packet, clnp_discard
* is called to discard the packet and send an ER. If
* clnp_fragment was called from clnp_output, then
* we generated the packet, and should not send an
* ER -- clnp_emit_er will check for this. Otherwise,
* the packet was fragmented during forwarding. In this
* case, we ought to send an ER back.
clnp_fragment(ifp
, m
, first_hop
, total_len
, segoff
, flags
, rt
)
struct ifnet
*ifp
; /* ptr to outgoing interface */
struct mbuf
*m
; /* ptr to packet */
struct sockaddr
*first_hop
; /* ptr to first hop */
int total_len
; /* length of datagram */
int segoff
; /* offset of segpart in hdr */
int flags
; /* flags passed to clnp_output */
struct rtentry
*rt
; /* route if direct ether */
struct clnp_fixed
*clnp
= mtod(m
, struct clnp_fixed
*);
int hdr_len
= (int)clnp
->cnf_hdr_len
;
int frag_size
= (SN_MTU(ifp
, rt
) - hdr_len
) & ~7;
if ((clnp
->cnf_type
& CNF_SEG_OK
) &&
(frag_size
> 8 || (frag_size
== 8 && !(total_len
& 7)))) {
struct mbuf
*hdr
= NULL
; /* save copy of clnp hdr */
struct mbuf
*frag_hdr
= NULL
;
struct mbuf
*frag_data
= NULL
;
struct clnp_segment seg_part
; /* segmentation header */
(void) bcopy(segoff
+ mtod(m
, caddr_t
), (caddr_t
)&seg_part
,
frag_base
= ntohs(seg_part
.cng_off
);
* Duplicate header, and remove from packet
if ((hdr
= m_copy(m
, 0, hdr_len
)) == NULL
) {
clnp_discard(m
, GEN_CONGEST
);
int remaining
, last_frag
;
struct mbuf
*mdump
= frag_hdr
;
printf("clnp_fragment: total_len %d:\n", total_len
);
printf("\tmbuf x%x, m_len %d\n",
tot_mlen
+= mdump
->m_len
;
printf("clnp_fragment: sum of mbuf chain %d:\n", tot_mlen
);
frag_size
= min(total_len
, frag_size
);
if ((remaining
= total_len
- frag_size
) == 0)
* If this fragment will cause the last one to
* be less than 8 bytes, shorten this fragment a bit.
* The obscure test on frag_size above ensures that
* frag_size will be positive.
printf("clnp_fragment: seg off %d, size %d, remaining %d\n",
ntohs(seg_part
.cng_off
), frag_size
, total_len
-frag_size
);
printf("clnp_fragment: last fragment\n");
* this is the last fragment; we don't need to get any other
/* duplicate header and data mbufs */
if ((frag_hdr
= m_copy(hdr
, 0, (int)M_COPYALL
)) == NULL
) {
clnp_discard(hdr
, GEN_CONGEST
);
if ((frag_data
= m_copy(m
, 0, frag_size
)) == NULL
) {
clnp_discard(hdr
, GEN_CONGEST
);
clnp
= mtod(frag_hdr
, struct clnp_fixed
*);
clnp
->cnf_type
|= CNF_MORE_SEGS
;
m_cat(frag_hdr
, frag_data
);
/* insert segmentation part; updated below */
bcopy((caddr_t
)&seg_part
, mtod(frag_hdr
, caddr_t
) + segoff
,
sizeof(struct clnp_segment
));
int derived_len
= hdr_len
+ frag_size
;
HTOC(clnp
->cnf_seglen_msb
, clnp
->cnf_seglen_lsb
, derived_len
);
if ((frag_hdr
->m_flags
& M_PKTHDR
) == 0)
panic("clnp_frag:lost header");
frag_hdr
->m_pkthdr
.len
= derived_len
;
/* compute clnp checksum (on header only) */
if (flags
& CLNP_NO_CKSUM
) {
HTOC(clnp
->cnf_cksum_msb
, clnp
->cnf_cksum_lsb
, 0);
iso_gen_csum(frag_hdr
, CLNP_CKSUM_OFF
, hdr_len
);
struct mbuf
*mdump
= frag_hdr
;
printf("clnp_fragment: sending dg:\n");
printf("\tmbuf x%x, m_len %d\n", mdump
, mdump
->m_len
);
error
= troll_output(ifp
, frag_hdr
, first_hop
, rt
);
error
= (*ifp
->if_output
)(ifp
, frag_hdr
, first_hop
, rt
);
* Tough situation: if the error occured on the last
* fragment, we can not send an ER, as the if_output
* routine consumed the packet. If the error occured
* on any intermediate packets, we can send an ER
* because we still have the original header in (m).
* The error was not on the last fragment. We must
* free hdr and m before returning
clnp_discard(hdr
, GEN_NOREAS
);
/* bump segment offset, trim data mbuf, and decrement count left */
* Decrement frag_size by some fraction. This will cause the
* next fragment to start 'early', thus duplicating the end
* of the current fragment. troll.tr_dup_size controls
* the fraction. If positive, it specifies the fraction. If
* negative, a random fraction is used.
if ((trollctl
.tr_ops
& TR_DUPEND
) && (!last_frag
)) {
int num_bytes
= frag_size
;
if (trollctl
.tr_dup_size
> 0)
num_bytes
*= trollctl
.tr_dup_size
;
num_bytes
*= troll_random();
seg_part
.cng_off
= htons(frag_base
);
clnp_discard(m
, GEN_SEGNEEDED
);
* PURPOSE: Attempt to reassemble a clnp packet given the current
* fragment. If reassembly succeeds (all the fragments
* are present), then return a pointer to an mbuf chain
* containing the reassembled packet. This packet will
* appear in the mbufs as if it had just arrived in
* If reassembly fails, then save this fragment and
* RETURNS: Ptr to assembled packet, or 0
* clnp_slowtimo can not affect this code because clnpintr, and thus
* this code, is called at a higher priority than clnp_slowtimo.
clnp_reass(m
, src
, dst
, seg
)
struct mbuf
*m
; /* new fragment */
struct iso_addr
*src
; /* src of new fragment */
struct iso_addr
*dst
; /* dst of new fragment */
struct clnp_segment
*seg
; /* segment part of fragment header */
register struct clnp_fragl
*cfh
;
/* look for other fragments of this datagram */
for (cfh
= clnp_frags
; cfh
!= NULL
; cfh
= cfh
->cfl_next
) {
if (seg
->cng_id
== cfh
->cfl_id
&&
iso_addrmatch1(src
, &cfh
->cfl_src
) &&
iso_addrmatch1(dst
, &cfh
->cfl_dst
)) {
printf("clnp_reass: found packet\n");
* There are other fragments here already. Lets see if
* this fragment is of any help
clnp_insert_frag(cfh
, m
, seg
);
if (m
= clnp_comp_pdu(cfh
)) {
register struct clnp_fixed
*clnp
= mtod(m
, struct clnp_fixed
*);
HTOC(clnp
->cnf_seglen_msb
, clnp
->cnf_seglen_lsb
,
printf("clnp_reass: new packet!\n");
* This is the first fragment. If src is not consuming too many
* resources, then create a new fragment list and add
* this fragment to the list.
/* TODO: don't let one src hog all the reassembly buffers */
if (!clnp_newpkt(m
, src
, dst
, seg
) /* || this src is a hog */) {
INCSTAT(cns_fragdropped
);
clnp_discard(m
, GEN_CONGEST
);
* PURPOSE: Create the necessary structures to handle a new
* fragmented clnp packet.
* RETURNS: non-zero if it succeeds, zero if fails.
* NOTES: Failure is only due to insufficient resources.
clnp_newpkt(m
, src
, dst
, seg
)
struct mbuf
*m
; /* new fragment */
struct iso_addr
*src
; /* src of new fragment */
struct iso_addr
*dst
; /* dst of new fragment */
struct clnp_segment
*seg
; /* segment part of fragment header */
register struct clnp_fragl
*cfh
;
register struct clnp_fixed
*clnp
;
clnp
= mtod(m
, struct clnp_fixed
*);
* Allocate new clnp fragl structure to act as header of all fragments
MGET(m0
, M_DONTWAIT
, MT_FTABLE
);
cfh
= mtod(m0
, struct clnp_fragl
*);
* Duplicate the header of this fragment, and save in cfh.
* Free m0 and return if m_copy does not succeed.
if ((cfh
->cfl_orighdr
= m_copy(m
, 0, (int)clnp
->cnf_hdr_len
)) == NULL
) {
/* Fill in rest of fragl structure */
bcopy((caddr_t
)src
, (caddr_t
)&cfh
->cfl_src
, sizeof(struct iso_addr
));
bcopy((caddr_t
)dst
, (caddr_t
)&cfh
->cfl_dst
, sizeof(struct iso_addr
));
cfh
->cfl_id
= seg
->cng_id
;
cfh
->cfl_ttl
= clnp
->cnf_ttl
;
cfh
->cfl_last
= (seg
->cng_tot_len
- clnp
->cnf_hdr_len
) - 1;
/* Insert into list of packets */
cfh
->cfl_next
= clnp_frags
;
/* Insert this fragment into list headed by cfh */
clnp_insert_frag(cfh
, m
, seg
);
* FUNCTION: clnp_insert_frag
* PURPOSE: Insert fragment into list headed by 'cf'.
* NOTES: This is the 'guts' of the reassembly algorithm.
* Each fragment in this list contains a clnp_frag
* structure followed by the data of the fragment.
* The clnp_frag structure actually lies on top of
* part of the old clnp header.
clnp_insert_frag(cfh
, m
, seg
)
struct clnp_fragl
*cfh
; /* header of list of packet fragments */
struct mbuf
*m
; /* new fragment */
struct clnp_segment
*seg
; /* segment part of fragment header */
register struct clnp_fixed
*clnp
; /* clnp hdr of fragment */
register struct clnp_frag
*cf
; /* generic fragment ptr */
register struct clnp_frag
*cf_sub
= NULL
; /* frag subsequent to new one */
register struct clnp_frag
*cf_prev
= NULL
; /* frag previous to new one */
u_short first
; /* offset of first byte of initial pdu*/
u_short last
; /* offset of last byte of initial pdu */
u_short fraglen
;/* length of fragment */
clnp
= mtod(m
, struct clnp_fixed
*);
CTOH(clnp
->cnf_seglen_msb
, clnp
->cnf_seglen_lsb
, fraglen
);
fraglen
-= clnp
->cnf_hdr_len
;
last
= (first
+ fraglen
) - 1;
printf("clnp_insert_frag: New fragment: [%d ... %d], len %d\n",
printf("clnp_insert_frag: current fragments:\n");
for (cf
= cfh
->cfl_frags
; cf
!= NULL
; cf
= cf
->cfr_next
) {
printf("\tcf x%x: [%d ... %d]\n", cf
, cf
->cfr_first
, cf
->cfr_last
);
if (cfh
->cfl_frags
!= NULL
) {
* Find fragment which begins after the new one
for (cf
= cfh
->cfl_frags
; cf
!= NULL
; cf_prev
= cf
, cf
= cf
->cfr_next
) {
if (cf
->cfr_first
> first
) {
printf("clnp_insert_frag: Previous frag is ");
printf("[%d ... %d]\n", cf_prev
->cfr_first
, cf_prev
->cfr_last
);
printf("clnp_insert_frag: Subsequent frag is ");
printf("[%d ... %d]\n", cf_sub
->cfr_first
, cf_sub
->cfr_last
);
* If there is a fragment before the new one, check if it
* overlaps the new one. If so, then trim the end of the
if (cf_prev
->cfr_last
> first
) {
u_short overlap
= cf_prev
->cfr_last
- first
;
printf("clnp_insert_frag: previous overlaps by %d\n",
* The new fragment is entirely contained in the
* preceeding one. We can punt on the new frag
/* Trim data off of end of previous fragment */
/* inc overlap to prevent duplication of last byte */
m_adj(cf_prev
->cfr_data
, -(int)overlap
);
cf_prev
->cfr_last
-= overlap
;
* For all fragments past the new one, check if any data on
* the new one overlaps data on existing fragments. If so,
* then trim the extra data off the end of the new one.
for (cf
= cf_sub
; cf
!= NULL
; cf
= cf
->cfr_next
) {
if (cf
->cfr_first
< last
) {
u_short overlap
= last
- cf
->cfr_first
;
printf("clnp_insert_frag: subsequent overlaps by %d\n",
* The new fragment is entirely contained in the
* succeeding one. This should not happen, because
* early on in this code we scanned for the fragment
* which started after the new one!
printf("clnp_insert_frag: internal error!\n");
/* Trim data off of end of new fragment */
/* inc overlap to prevent duplication of last byte */
* Insert the new fragment beween cf_prev and cf_sub
* Note: the clnp hdr is still in the mbuf.
* If the data of the mbuf is not word aligned, shave off enough
* so that it is. Then, cast the clnp_frag structure on top
* The clnp_hdr will not be used again (as we already have
* Save in cfr_bytes the number of bytes to shave off to get to
* the data of the packet. This is used when we coalesce fragments;
* the clnp_frag structure must be removed before joining mbufs.
/* determine if header is not word aligned */
/* bytes is number of bytes left in front of data */
bytes
= clnp
->cnf_hdr_len
- pad
;
printf("clnp_insert_frag: clnp x%x requires %d alignment\n",
/* make it word aligned if necessary */
cf
= mtod(m
, struct clnp_frag
*);
printf("clnp_insert_frag: cf now x%x, cfr_bytes %d\n", cf
,
* The data is the mbuf itself, although we must remember that the
* first few bytes are actually a clnp_frag structure
* FUNCTION: clnp_comp_pdu
* PURPOSE: Scan the list of fragments headed by cfh. Merge
* any contigious fragments into one. If, after
* traversing all the fragments, it is determined that
* the packet is complete, then return a pointer to
* the packet (with header prepended). Otherwise,
* RETURNS: NULL, or a pointer to the assembled pdu in an mbuf chain.
* SIDE EFFECTS: Will colapse contigious fragments into one.
* NOTES: This code assumes that there are no overlaps of
struct clnp_fragl
*cfh
; /* fragment header */
register struct clnp_frag
*cf
= cfh
->cfl_frags
;
while (cf
->cfr_next
!= NULL
) {
register struct clnp_frag
*cf_next
= cf
->cfr_next
;
printf("clnp_comp_pdu: comparing: [%d ... %d] to [%d ... %d]\n",
cf
->cfr_first
, cf
->cfr_last
, cf_next
->cfr_first
,
if (cf
->cfr_last
== (cf_next
->cfr_first
- 1)) {
* Merge fragment cf and cf_next
* - trim clnp_frag structure off of cf_next
struct clnp_frag cf_next_hdr
;
struct clnp_frag
*next_frag
;
next_frag
= cf_next
->cfr_next
;
printf("clnp_comp_pdu: merging fragments\n");
printf("clnp_comp_pdu: 1st: [%d ... %d] (bytes %d)\n",
cf
->cfr_first
, cf
->cfr_last
, cf
->cfr_bytes
);
printf("\tmbuf x%x, m_len %d\n", mdump
, mdump
->m_len
);
printf("\ttotal len: %d\n", l
);
printf("clnp_comp_pdu: 2nd: [%d ... %d] (bytes %d)\n",
cf_next
->cfr_first
, cf_next
->cfr_last
, cf_next
->cfr_bytes
);
mdump
= cf_next
->cfr_data
;
printf("\tmbuf x%x, m_len %d\n", mdump
, mdump
->m_len
);
printf("\ttotal len: %d\n", l
);
cf
->cfr_last
= cf_next
->cfr_last
;
* After this m_adj, the cf_next ptr is useless because we
* have adjusted the clnp_frag structure away...
printf("clnp_comp_pdu: shaving off %d bytes\n",
m_adj(cf_next_hdr
.cfr_data
, (int)cf_next_hdr
.cfr_bytes
);
m_cat(cf
->cfr_data
, cf_next_hdr
.cfr_data
);
cf
->cfr_next
= next_frag
;
struct mbuf
*mdump
= cf
->cfr_data
;
printf("clnp_comp_pdu: first frag now: [%d ... %d]\n", cf
->cfr_first
,
printf("clnp_comp_pdu: data for frag:\n");
printf("mbuf x%x, m_len %d\n", mdump
, mdump
->m_len
);
/* dump_buf(mtod(mdump, caddr_t), mdump->m_len);*/
/* Check if datagram is complete */
if ((cf
->cfr_first
== 0) && (cf
->cfr_last
== cfh
->cfl_last
)) {
* We have a complete pdu!
* - Remove the frag header from (only) remaining fragment
* (which is not really a fragment anymore, as the datagram is
* - Prepend a clnp header
struct mbuf
*data
= cf
->cfr_data
;
struct mbuf
*hdr
= cfh
->cfl_orighdr
;
printf("clnp_comp_pdu: complete pdu!\n");
m_adj(data
, (int)cf
->cfr_bytes
);
struct mbuf
*mdump
= hdr
;
printf("clnp_comp_pdu: pdu is:\n");
printf("mbuf x%x, m_len %d\n", mdump
, mdump
->m_len
);
/* dump_buf(mtod(mdump, caddr_t), mdump->m_len);*/
* Remove cfh from the list of fragmented pdus
clnp_frags
= cfh
->cfl_next
;
for (scan
= clnp_frags
; scan
!= NULL
; scan
= scan
->cfl_next
) {
if (scan
->cfl_next
== cfh
) {
scan
->cfl_next
= cfh
->cfl_next
;
* PURPOSE: generate a pseudo-random number between 0 and 1
* RETURNS: the random number
* NOTES: This is based on the clock.
extern struct timeval time
;
long t
= time
.tv_usec
% 100;
return((float)t
/ (float) 100);
* PURPOSE: Do something sneaky with the datagram passed. Possible
* Trim some number of bytes from the packet
* Munge some byte in the packet
* RETURNS: 0, or unix error code
* NOTES: The operation of this procedure is regulated by the
* troll control structure (Troll).
troll_output(ifp
, m
, dst
, rt
)
if (trollctl
.tr_ops
& TR_DUPPKT
) {
* Duplicate every Nth packet
float f_freq
= troll_cnt
* trollctl
.tr_dup_freq
;
int i_freq
= troll_cnt
* trollctl
.tr_dup_freq
;
struct mbuf
*dup
= m_copy(m
, 0, (int)M_COPYALL
);
err
= (*ifp
->if_output
)(ifp
, dup
, dst
, rt
);
err
= (*ifp
->if_output
)(ifp
, m
, dst
, rt
);
} else if (trollctl
.tr_ops
& TR_DROPPKT
) {
} else if (trollctl
.tr_ops
& TR_CHANGE
) {
struct clnp_fixed
*clnp
= mtod(m
, struct clnp_fixed
*);
err
= (*ifp
->if_output
)(ifp
, m
, dst
, rt
);
err
= (*ifp
->if_output
)(ifp
, m
, dst
, rt
);