- 
                Notifications
    You must be signed in to change notification settings 
- Fork 232
[draft] feat(usbgadget): share JetKVM's internet via USB ethernet #459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            dlorch
  wants to merge
  7
  commits into
  jetkvm:dev
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
dlorch:feat/share-internet-usb
  
      
      
   
  
    
  
  
  
 
  
      
    base: dev
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9b75613
              
                add USB gadget ethernet configurations
              
              
                dlorch efde7dc
              
                Remove logging statemets
              
              
                dlorch a467793
              
                Improve interface state log message
              
              
                dlorch b512e4d
              
                Enable Ipv4 forwarding
              
              
                dlorch 0317ccc
              
                Make USB network settings configurable
              
              
                dlorch 9176655
              
                Initialize USB ethernet
              
              
                dlorch ecef1ea
              
                Fix error messages
              
              
                dlorch File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package network | ||
|  | ||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| ) | ||
|  | ||
| const ( | ||
| procIpv4ForwardPath = "/proc/sys/net/ipv4/ip_forward" | ||
| ) | ||
|  | ||
| func (s *NetworkInterfaceState) UsbNetworkConfig() *UsbNetworkConfig { | ||
| return s.usbNetConfig | ||
| } | ||
|  | ||
| func (s *NetworkInterfaceState) reconfigureNat(wantNat bool, sourceAddr string) error { | ||
| scopedLogger := s.l.With().Str("iface", s.interfaceName).Logger() | ||
|  | ||
| if !wantNat { | ||
| if s.natEnabled { | ||
| scopedLogger.Info().Msg("disabling NAT") | ||
| err := disableNat() | ||
| if err != nil { | ||
| s.l.Error().Err(err).Msg("failed to disable NAT") | ||
|         
                  dlorch marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| } | ||
|         
                  dlorch marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| } | ||
| return nil | ||
| } | ||
|  | ||
| if wantNat && s.IsOnline() { | ||
| scopedLogger.Info().Msg("enabling NAT") | ||
| err := enableNat(sourceAddr, s.interfaceName, s.IPv4String()) | ||
| if err != nil { | ||
| s.l.Error().Err(err).Msg("failed to enable NAT") | ||
|         
                  dlorch marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| } | ||
|         
                  dlorch marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| s.natEnabled = true | ||
| return nil | ||
| } | ||
|  | ||
| return nil | ||
| } | ||
|  | ||
| func enableNat(sourceAddr string, oIfName string, snatToAddr string) error { | ||
| if err := os.WriteFile(procIpv4ForwardPath, []byte("1"), 0644); err != nil { | ||
| return fmt.Errorf("failed to write %s: %w", procIpv4ForwardPath, err) | ||
| } | ||
|  | ||
| if err := exec.Command("nft", "add table nat").Run(); err != nil { | ||
| return fmt.Errorf("failed to add table nat: %w", err) | ||
| } | ||
|  | ||
| if err := exec.Command("nft", "flush table nat").Run(); err != nil { | ||
| return fmt.Errorf("failed to flush table nat: %w", err) | ||
| } | ||
|  | ||
| if err := exec.Command("nft", "add chain nat postrouting { type nat hook postrouting priority 100 ; }").Run(); err != nil { | ||
| return fmt.Errorf("failed to add chain nat: %w", err) | ||
| } | ||
|  | ||
| if err := exec.Command("nft", "add rule nat postrouting ip saddr", sourceAddr, "oif", oIfName, "snat to", snatToAddr).Run(); err != nil { | ||
| return fmt.Errorf("failed to add postrouting rule: %w", err) | ||
| } | ||
|  | ||
| return nil | ||
| } | ||
|  | ||
| func disableNat() error { | ||
| if err := os.WriteFile(procIpv4ForwardPath, []byte("0"), 0644); err != nil { | ||
| return fmt.Errorf("failed to write %s: %w", procIpv4ForwardPath, err) | ||
| } | ||
|  | ||
| if err := exec.Command("nft", "delete table nat").Run(); err != nil { | ||
| return fmt.Errorf("failed to run nft: %w", err) | ||
| } | ||
|  | ||
| return nil | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package usbgadget | ||
|  | ||
| const ( | ||
| usbEthernetDevice = "usb0" | ||
| ) | ||
|  | ||
| // Ethernet Control Model (ECM) | ||
| var ethernetEcmConfig = gadgetConfigItem{ | ||
| order: 4000, | ||
| path: []string{"functions", "ecm.usb0"}, | ||
| configPath: []string{"ecm.usb0"}, | ||
| attrs: gadgetAttributes{ | ||
| "host_addr": "", // MAC address of target host (randomly select) | ||
| "dev_addr": "", // MAC address of JetKVM (randomly select) | ||
| }, | ||
| } | ||
|  | ||
| // Ethernet Emulation Model (EEM) | ||
| var ethernetEemConfig = gadgetConfigItem{ | ||
| order: 4001, | ||
| path: []string{"functions", "eem.usb0"}, | ||
| configPath: []string{"eem.usb0"}, | ||
| attrs: gadgetAttributes{ | ||
| "host_addr": "", // MAC address of target host (randomly select) | ||
| "dev_addr": "", // MAC address of JetKVM (randomly select) | ||
| }, | ||
| } | ||
|  | ||
| // Network Control Model (NCM) | ||
| var ethernetNcmConfig = gadgetConfigItem{ | ||
| order: 4001, | ||
| path: []string{"functions", "ncm.usb0"}, | ||
| configPath: []string{"ncm.usb0"}, | ||
| attrs: gadgetAttributes{ | ||
| "host_addr": "", // MAC address of target host (randomly select) | ||
| "dev_addr": "", // MAC address of JetKVM (randomly select) | ||
| }, | ||
| } | ||
|  | ||
| // Remote Network Driver Interface Specification (RNDIS) | ||
| var ethernetRndisConfig = gadgetConfigItem{ | ||
| order: 4001, | ||
| path: []string{"functions", "rndis.usb0"}, | ||
| configPath: []string{"rndis.usb0"}, | ||
| attrs: gadgetAttributes{ | ||
| "host_addr": "", // MAC address of target host (randomly select) | ||
| "dev_addr": "", // MAC address of JetKVM (randomly select) | ||
| }, | ||
| } | ||
|  | ||
| func (u *UsbGadget) UsbEthernetEnabled() bool { | ||
| return u.isGadgetConfigItemEnabled("ecm") || | ||
| u.isGadgetConfigItemEnabled("eem") || | ||
| u.isGadgetConfigItemEnabled("ncm") || | ||
| u.isGadgetConfigItemEnabled("rndis") | ||
| } | ||
|  | ||
| func (u *UsbGadget) UsbEthernetDevice() string { | ||
| return usbEthernetDevice | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.